ToolTip on DataGridView

2020-07-24 06:10发布

问题:

I've got a ToolTip showing when hovering over a row in my DataGridView - Works great except for the ToolTip flickering when over a row that displays it.

Private Sub DataGridView1_MouseHover(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles DataGridView1.MouseMove
    Dim hit As DataGridView.HitTestInfo = DataGridView1.HitTest(e.X, e.Y)

    If hit.Type = DataGridViewHitTestType.Cell Then

        If Not hit Is m_HoveredItem Then
            Me.ToolTip2.Hide(Me.DataGridView1)
            m_HoveredItem = hit
            If hit Is Nothing Then
                Me.ToolTip2.SetToolTip(Me.DataGridView1, "")
            Else
                'Me.ToolTip2.SetToolTip(Me.DataGridView1, ConnectedUsers(Me.DataGridView1.Rows(hit.RowIndex).Cells("Database").Value, Instance))
                Me.ToolTip2.Show(ConnectedUsers(Me.DataGridView1.Rows(hit.RowIndex).Cells("Database").Value, Instance), DataGridView1, e.X, e.Y)
            End If
        End If

    End If

End Sub

I used a similar method for a ListView which worked great:

Private m_HoveredItem As ListViewItem
Private Sub lv_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs)

Dim lvi As ListViewItem = Me.LVDatabases.GetItemAt(e.X, e.Y)

If Not lvi Is m_HoveredItem Then
    Me.ToolTip2.Hide(Me.LVDatabases)
    m_HoveredItem = lvi
    If lvi Is Nothing Then
        Me.ToolTip2.SetToolTip(Me.LVDatabases, "")
    Else
        Me.ToolTip2.SetToolTip(Me.LVDatabases, ConnectedUsers(Me.LVDatabases.GetItemAt(e.X, e.Y).Text, Instance))
    End If
End If
End Sub

回答1:

Private cellColumnIndex As Integer = -1, cellRowIndex As Integer = -1
Private Sub testDataGridView_CellMouseMove(ByVal sender As Object, ByVal e As DataGridViewCellMouseEventArgs) Handles DataGridView1.CellMouseMove
    If e.ColumnIndex <> Me.cellColumnIndex OrElse e.RowIndex <> Me.cellRowIndex Then
        Me.ToolTip2.Hide(Me.DataGridView1)
        Me.cellColumnIndex = e.ColumnIndex
        Me.cellRowIndex = e.RowIndex
        If Me.cellColumnIndex >= 0 AndAlso Me.cellRowIndex >= 0 Then
            Me.ToolTip2.SetToolTip(Me.DataGridView1, ConnectedUsers(Me.DataGridView1.Rows(e.RowIndex).Cells("Database").Value, Instance))
        End If
    End If
End Sub


回答2:

For datagridview to show your tooltiptext you should set the property "showcelltooltips" to false, then only your tool tip text will be shown



回答3:

FWIW, I believe this is a bug in the DataGridView on Windows 7. We used this technique in an app written in C# running on Windows XP. Everything worked fine, but when we moved the app to Windows 7 we noticed this flickering issue. After investigation, we found repeated calls to our MouseMove event handler. I added a simple trace line at the top of the handler that just reported the location of the mouse at Event triggering, e.X and e.Y. We saw continuous repeated execution at the same point, even when taking hand off mouse and backing chair away from desk.

Investigation on MSDN at the time indicated that the event would not be triggered when the mouse did not move. This certainly appears to be incorrect for the DataGridView. We did not notice this issue on any other controls, but we did not do exhaustive testing.

.Net 3.5 Client Profile, C# WinForms app



标签: vb.net