我有一个关于 winform 的问题: 我需要文本框单元格的选择开始和长度。 就像现在一样,我总是得到上一次编辑的开始和长度(例如,这些值是最新编辑后面的一个编辑)。
Private Sub oParams_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles oParams.EditingControlShowing
If TypeOf e.Control Is DataGridViewTextBoxEditingControl Then
Dim tbec As DataGridViewTextBoxEditingControl = CType(e.Control, DataGridViewTextBoxEditingControl)
Dim cellText As String = tbec.Text
selCellSt = tbec.SelectionStart
selCellLen = tbec.SelectionLength
'MsgBox("start: " & tbec.SelectionStart & vbNewLine & "Length: " & tbec.SelectionLength)
End If
End Sub
按下按钮时我使用这个:
Private Sub oParams_CellContentClick(sender As Object, e As DataGridViewCellEventArgs) Handles oParams.CellContentClick
If e.ColumnIndex = 4 Then
MsgBox("sel start: " & selCellSt & vbNewLine & "sel lenght: " & selCellLen)
End If
有人有解决方案以便获得最新选择的值吗?
视频作为我的意思的参考: https://youtu.be/mcKWuyA66aA
提前谢谢你
仅当显示编辑控件时,选择单元格内的文本才有意义。尝试在
CellContentClick
事件上获取该数据是没有意义的,因为当显示编辑控件时,无法选择单元格内容,也无法单击它。您需要做的是处理在显示控件之前引发的 EditingControlShowing
事件,然后处理该控件的相应事件,例如
Private WithEvents editingControl As TextBox
Private Sub DataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCellAddress.X = 4 Then
editingControl = DirectCast(e.Control, TextBox)
End If
End Sub
Private Sub DataGridView1_CellEndEdit(sender As Object, e As DataGridViewCellEventArgs) Handles DataGridView1.CellEndEdit
editingControl = Nothing
End Sub
Private Sub EditingControl_Enter(sender As Object, e As EventArgs) Handles editingControl.Enter
'...
End Sub
Private Sub EditingControl_Leave(sender As Object, e As EventArgs) Handles editingControl.Leave
'...
End Sub
请注意,当
TextBox
或 SelectionStart
属性更改时,SelectionLength
控件不会引发任何事件,因此没有简单的方法来获取这些属性的实时值。您可以处理所有更改时可能引发的事件,即与鼠标和键盘相关的事件。