Friday, February 6, 2015

How to display tooltip on mouse hover in particular cell or column cell?

Scenario:

Today I got an assignment to display tooltip on particular column cell depending on its text that contain some specific prefix to create different tool tip text.  The grid control was used is Infragistics UltraWinGrid to populate data to user.

Solution/Description:

Most of the time I work with DevExpress controls and default win form controls, there we have different approach to get the grid element when hovering mouse over the grid element.  These controls use HitTestInfo to determine that which grid element currently hovered. While looking for the solution I found one Stackoverflow thread - DataGridView.HitTestInfo equivalent in Infragistics UltraWinGrid? . I am also hunting for the same question’s answer that does Infragistics’s UltraGrid control provides functionality similar to that of DataGridView.HitTestInfo?

Source - Task-based Tutorial - How to use ToolTips with the Infragistics UltraWinGrid

I found out that Infragistics controls don't convert the coordinates, but they use a special Infragistics grid event (MouseEnterElement) to get the element, which the mouse currently hovers over. Also with the Infragistics UltraWinGrid-control it required to keep track of the grid-row, over which the cursor is hovering. In contrast to our generic solution, the position is stored in a data-type provided by the UltraWinGrid-control and not in a Point-structure: 
Public Class Form1
    Inherits System.Windows.Forms.Form 
   ...  
    'Holds the row/column-coordinates of the cell on which
    'the mouse is hovering...
    Private mCurrentCell As Infragistics.Win _
                .UltraWinGrid.UltraGridCell = 
Nothing
  ...
The way of implement for the generic grid-control (in the section on "How to use ToolTips with grid-controls"), the whole logic, to keep track of the current mouse-position in terms of grid-coordinates (row/column) and to provide the tooltip with the content of the current grid-element, is located in the MouseEnterMouseLeave and MouseMove event-handlers. This is slightly different for the UltraWinGrid-control. Of course, the needed logic is also coupled to mouse-events, but the UltraWinGrid-control provides additional mouse-related events, which make the integration even easier. 
Code snippet to show tooltip while using UltraWinGrid:
private void ultraGrid1_MouseEnterElement(object sender, UIElementEventArgs e)
{
    if (e.Element is RowUIElement)
    {
        if (e.Element.SelectableItem is UltraGridRow)
        {
            UltraGridRow ultraGridRow = (UltraGridRow)e.Element.SelectableItem;

            if (ultraGridRow.Band.Key == PaletteZoneHVACGroupingConstants.BAND_PARENT)
            {
                toolTip.ToolTipText = "Drag-n-drop here";
                toolTip.Show();
            }
        }               
    }
    else if(e.Element is CellUIElement)
    {
        // Get a refernce to the column.
        UltraGridColumn column = (UltraGridColumn)e.Element.GetContext();
        if (column != null && column.Key == ColumnName.DESIGNTEMPLATE)
        {
            UltraGridCell cell = e.Element.SelectableItem as UltraGridCell;
            if (cell.Row.Band.Key == Constants.BAND_PARENT)
            {
                string toolTipMessage = string.Empty;
                if (cell.Text.StartsWith(Constants.TextDesginChangePrefix))
                {
                    toolTipMessage = ResourceMessage.GetMessage(ResourceConstants. DESGINS_CHANGE);
                }
                else if (cell.Text.StartsWith(Constants.TextParametersChangePrefix))
                {
                    toolTipMessage = ResourceMessage.GetMessage(ResourceConstants.DESGINS_PARAMETERS_CHANGE);
                }
               
                if (!string.IsNullOrWhiteSpace(toolTipMessage))
                {
                    toolTip.ToolTipText = toolTipMessage;
                    toolTip.Show();
                }
            }
        }
    }
}

private void ultraGrid1_MouseLeaveElement(object sender, UIElementEventArgs e)
{
    toolTip.Hide();
}






No comments :

Post a Comment