DevExpress WinForms Cheat Sheets, Best Practices and Troubleshooting
Most DevExpress UI controls consist of multiple visual elements (e.g., bands, panels, buttons). These compound controls feature similar CalcHitInfo methods. They allow you to check what visual element is located under the mouse pointer. You can call this method inside click event handlers to show menus or tooltips for elements that do not natively support them.
The list of CalcHitInfo methods for major DevExpress WinForms controls:
- AccordionControl.CalcHitInfo
- RibbonControl.CalcHitInfo
- TileControl.CalcHitInfo
- GridView.CalcHitInfo
- LayoutControl.CalcHitInfo
- XtraTabControl.CalcHitInfo
- TreeList.CalcHitInfo
You can find a complete list of controls that support the CalcHitInfo method in our documentation by entering "CalcHitInfo" in the API list filter: WinForms Controls - CalcHitInfo
Examples
Handle a click on a Data Grid band button
The Data Grid Band Panel Button does not do anything when users click it. You can change that if you handle any Mouse~ event and use the BandedGridView.CalcHitInfo method to determine which visual element a user has clicked.
C#private void bandedGridView1_MouseDown(object sender, MouseEventArgs e)
{
var view = sender as BandedGridView;
var hitInfo = view.CalcHitInfo(e.Location);
if (hitInfo.HitTest == DevExpress.XtraGrid.Views.BandedGrid.ViewInfo.BandedGridHitTest.BandButton)
{
//your code
}
}
Show a context menu for the XtraTabControl page header
To accomplish this task, use the XtraTabControl.CalcHitInfo method to determine whether the TabControl's page header is under the current mouse pointer position. If yes, show a context menu built with our WinForms Popup Menu. Refer to the following cheat sheet to learn more about our menus: DevExpress WinForms Cheat Sheets - Context Menus.
C#XtraTabPage contextPage;
private void xtraTabControl1_MouseUp(object sender, System.Windows.Forms.MouseEventArgs e) {
if(e.Button == MouseButtons.Right) {
XtraTabControl tabCtrl = sender as XtraTabControl;
Point pt = MousePosition;
XtraTabHitInfo info = tabCtrl.CalcHitInfo(tabCtrl.PointToClient(pt));
if(info.HitTest == XtraTabHitTest.PageHeader) {
contextPage = info.Page;
popupMenu1.ShowPopup(pt);
}
}
}
Show a tooltip for a TreeList cell
Handle the GetActiveObjectInfo event of our ToolTipController component to create the ToolTipControlInfo object (see DevExpress WinForms Cheat Sheet - Tooltips and Hints). To retrieve the hovered TreeList cell, use the TreeList.CalcHitInfo method.
C#private void toolTipController1_GetActiveObjectInfo(object sender, DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs e) {
if(e.SelectedControl is DevExpress.XtraTreeList.TreeList) {
TreeList tree = (TreeList)e.SelectedControl;
TreeListHitInfo hit = tree.CalcHitInfo(e.ControlMousePosition);
if(hit.HitInfoType == HitInfoType.Cell) {
object cellInfo = new TreeListCellToolTipInfo(hit.Node, hit.Column, null);
string toolTip = string.Format("{0} (Column: {1}, Node ID: {2})", hit.Node[hit.Column], hit.Column.Caption, hit.Node.Id);
e.Info = new DevExpress.Utils.ToolTipControlInfo(cellInfo, toolTip);
}
}
}