Back to Devexpress

How to: Display a Tooltip for a Particular Visual Element

windowsforms-1964-common-features-tooltip-management-how-to-display-a-tooltip-for-a-particular-visual-element.md

latest4.8 KB
Original Source

How to: Display a Tooltip for a Particular Visual Element

  • Oct 29, 2021
  • 3 minutes to read

DevExpress controls can consist of multiple visual elements. For example, the grid control consists of column headers, band headers, etc. Certain visual elements already have predefined tooltips. For example, if the text in a grid control’s column header is trimmed, the tooltip displays the entire text. You can also provide custom tooltips for visual elements. For example, you can use the GridColumn.ToolTip or GridBand.ToolTip property to provide a custom tooltip for a column or band header.

If a visual element does not have a dedicated property, you can handle the ToolTipController.GetActiveObjectInfo event to provide a tooltip. Event arguments allow you to determine the visual element under the mouse pointer and specify the tooltip.

Example: Display a Tooltip for a Row Indicator in the Grid Control

This example shows how to display a row number in a tooltip when the mouse pointer hovers a row indicator.

To display the tooltips, do the following:

csharp
using DevExpress.Utils;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;

private void toolTipController1_GetActiveObjectInfo(object sender, 
ToolTipControllerGetActiveObjectInfoEventArgs e) {
    if(e.SelectedControl != gridControl1) return;

    ToolTipControlInfo info = null;
    // Get the view at the current mouse position.
    GridView view = gridControl1.GetViewAt(e.ControlMousePosition) as GridView;
    if(view == null) return;
    // Get the information about the visual element at the current mouse position.
    GridHitInfo hi = view.CalcHitInfo(e.ControlMousePosition);
    // Display a hint for a row indicator.
    if(hi.HitTest == GridHitTest.RowIndicator) {
        // Create an object that uniquely identifies a row indicator.
        object o = hi.HitTest.ToString() + hi.RowHandle.ToString();
        string text = "Row "+ hi.RowHandle.ToString();
        info = new ToolTipControlInfo(o, text);         
    }
    // Assign the tooltip information if applicable; otherwise, preserve the default tooltip.
    if (info != null)
        e.Info = info;
}
vb
Imports DevExpress.XtraGrid.Views.Grid
Imports DevExpress.XtraGrid.Views.Grid.ViewInfo

Private Sub ToolTipController1_GetActiveObjectInfo(ByVal sender As Object, _ 
ByVal e As DevExpress.Utils.ToolTipControllerGetActiveObjectInfoEventArgs) _
Handles ToolTipController1.GetActiveObjectInfo
    If Not e.SelectedControl Is gridControl1 Then Return

    Dim info As ToolTipControlInfo = Nothing
    ' Get the view at the current mouse position.
    Dim view As GridView = gridControl1.GetViewAt(e.ControlMousePosition)
    If view Is Nothing Then Return
    ' Get the information about the visual element at the current mouse position.
    Dim hi As GridHitInfo = view.CalcHitInfo(e.ControlMousePosition)
    ' Display a hint for a row indicator.
    If hi.HitTest = GridHitTest.RowIndicator Then
        ' Create an object that uniquely identifies a row indicator.
        Dim o As Object = hi.HitTest.ToString() + hi.RowHandle.ToString()
        Dim text As String = "Row " + hi.RowHandle.ToString()
        info = New ToolTipControlInfo(o, text)
    End If
    ' Assign the tooltip information if applicable; otherwise, preserve the default tooltip.
    If Not info Is Nothing Then e.Info = info
End Sub