Windows表单工具提示在初始单元格编辑

时间:2015-12-14 16:13:19

标签: vb.net datagridview tooltip

当用户正在编辑单元格时,我试图在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

1 个答案:

答案 0 :(得分:1)

重新创建问题后,我可以看到这里的电话:

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

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

如果您将此通话(在所有位置)更改为:

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

效果很好。