Windows 表单工具提示不会在初始单元格编辑时显示在 datagridview 上

Windows Form Tooltip won't display on datagridview on initial cell edit

我正在尝试在用户编辑单元格时在 datagridview 上显示工具提示。我的问题是第一次编辑单元格时工具提示不显示。例如,如果我在单元格中键入“=2*10”,工具提示不会显示,BUT 如果我要离开单元格,重新输入并继续键入然后工具提示出现(带有正确的文本和正确的位置)。所以我想知道为什么工具提示没有出现在初始编辑中。 C# 中的答案是可以接受的,并且更改 C# 和 VB 之间的语法是微不足道的。在这种情况下,代码中的变量'obj'引用了一个datagridview对象。

' obj is datagridview object 

' hide tool tip on Enter key press
If keyData = Keys.Enter Then
        ToolTip1.Hide(obj)
        obj.ShowCellToolTips = True

' Check if cell is empty
ElseIf (obj.Item(obj.CurrentCell.ColumnIndex, obj.CurrentRow.Index).Value IsNot Nothing) Then
        Debug.Print(obj.Item(obj.CurrentCell.ColumnIndex, obj.CurrentRow.Index).Value.ToString)

        ' Check if current cell starts with "="
        If obj.Item(obj.CurrentCell.ColumnIndex, obj.CurrentRow.Index).Value.ToString.StartsWith("=") Then
            If obj.IsCurrentCellInEditMode Then

                Dim RowHeight1 As Integer = obj.Rows(obj.CurrentRow.Index).Height
                Dim CellRectangle1 As Rectangle = obj.GetCellDisplayRectangle(obj.CurrentCell.ColumnIndex, obj.CurrentRow.Index, False)

                CellRectangle1.X += obj.Left
                CellRectangle1.Y += obj.Top 

                Dim displayPoint As Point = PointToScreen(New Point(CellRectangle1.X, CellRectangle1.Y))

                obj.CommitEdit(DataGridViewDataErrorContexts.Commit)
                obj.ShowCellToolTips = False
                ToolTip1.Show("This is a test", obj, displayPoint)
            End If

        Else 
            ToolTip1.Hide(obj)
            obj.ShowCellToolTips = True
        End If
End If

重现您的问题后,我可以在此处看到调用:

obj.Item(obj.CurrentCell.ColumnIndex, obj.CurrentRow.Index).Value

未检索到您期望的值。它提供未编辑的值,即 edit/validation 之后保存的值。

如果您将此调用(在 所有 处)更改为:

obj.Item(obj.CurrentCell.ColumnIndex, obj.CurrentRow.Index).EditedFormattedValue

效果很好。