ComponentOne FlexGrid for WinForms
Step 5 of 5: Implement ToolTips
FlexGrid for WinForms Tutorials > Outline Tutorial > Step 5 of 5: Implement ToolTips

To conclude this tutorial, we will add ToolTips to the outline. The ToolTips will display the text that was stored in each row's UserDataproperty by the GetXMLDataroutine described above. The ToolTips will show the contents of the "Name" node when the user moves the mouse over its parent node. This is useful when the parent node is collapsed and the "Name" node is not visible.

  1. Add a ToolTipcontrol to the form.
  2. Add the following code to handle the grid's MouseMoveevent:

    To write code in Visual Basic

    Visual Basic
    Copy Code
    Private Sub C1FlexGrid1_MouseMove(ByVal sender As Object, ByVal e As MouseEventArgs) Handles C1FlexGrid1.MouseMove
     
        ' Check the ToolTip for this cell.
        Dim tip As String
        If C1FlexGrid1.MouseCol = 0 And C1FlexGrid1.MouseRow > 0 Then
            tip = C1FlexGrid1.Rows(C1FlexGrid1.MouseRow).UserData
     
            ' Set it if it is different from the current ToolTip.
            If tip <> ToolTip1.GetToolTip(C1FlexGrid1) Then
                ToolTip1.SetToolTip(C1FlexGrid1, tip)
            End If
        End If
    End Sub
    

    To write code in C#

    C#
    Copy Code
    private void c1FlexGrid1_MouseMove( object sender, MouseEventArgs e)
    {
        // Check the ToolTip for this cell.
        string  tip;
        if ( c1FlexGrid1.MouseCol == 0 && c1FlexGrid1.MouseRow > 0 ) 
        {
            tip = (string)c1FlexGrid1.Rows[c1FlexGrid1.MouseRow].UserData;
     
            // Set it if it is different from the current ToolTip.
            if ( tip != toolTip1.GetToolTip(c1FlexGrid1) ) 
            {
                toolTip1.SetToolTip(c1FlexGrid1, tip);
            }
        }
    }
    

Run the program and observe the following:

The code starts by checking the cell under the mouse using the MouseRow and MouseCol properties. If the mouse is over the first column on a row that contains text for the ToolTip, it retrieves the text. Otherwise, the ToolTip text is set to Nothing.


Then the routine compares the new and current ToolTip text, and updates the text if necessary, by calling the SetToolTip method on the ToolTip control.

This concludes this tutorial. You can extend this project in many ways, including saving edits back into the XML document, adding, deleting, and moving nodes, using different styles for different types of data, and so on.

See Also