VB.NET 2010 DataGridView 通过 EditingControlShowing 事件处理按键 [英] VB.NET 2010 DataGridView Handling Keypress via EditingControlShowing Event
问题描述
我第一次使用 DataGridView,虽然我有很多问题,但这个最新问题让我很烦恼.
I am working with a DataGridView for the first time and while I have MANY questions, this latest issue is vexing me.
问题摘要:我有一个 DataGridView (dgv),我定义了一组列.有些只读有些可编辑.
对于可编辑的列,我需要发生四件事.
1) 允许数字输入
2) 最多允许 2 位数字
3) 零填充任何条目 <2 位数字
Summary of issue:
I have a DataGridView (dgv) which I have a set of columns defined. Some readonly some editable.
For the editable columns I need four things to occur.
1) Allow Numeric entry
2) Allow maximum of 2 digits
3) Zero Pad any entries <2 digits
4) 我的问题:
如果用户输入一个两位数的数字,我想检测该数字并将 TAB 移到下一列.我不能让它工作.
4) My ISSUE:
If the user types in a two digit number, I want to detect that and TAB to the next column. I cannot get this to work.
示例代码(省略了一些已知的工作项目):
Sample code (with some known working items left out):
Private Sub dgvDiary_EditingControlShowing(sender As Object, e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles dgvDiary.EditingControlShowing
Dim txtEdit As TextBox = e.Control
txtEdit.MaxLength = 2
'remove any existing handler
RemoveHandler txtEdit.KeyPress, AddressOf txtdgvDiaryEdit_Keypress
AddHandler txtEdit.KeyPress, AddressOf txtdgvDiaryEdit_Keypress
End Sub
Private Sub txtdgvDiaryEdit_Keypress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs)
'Test for numeric value or backspace
If IsNumeric(e.KeyChar.ToString()) _
Or e.KeyChar = ChrW(Keys.Back) Then
e.Handled = False 'if numeric
Else
e.Handled = True 'if non numeric
End If
'If user typed in 2 characters, move on!
'Don't work!
If Strings.Len(Me.dgvDiary.Rows(Me.dgvDiary.CurrentRow.Index).Cells(Me.dgvDiary.CurrentCell.ColumnIndex).Value) = 2 Then
SendKeys.Send("{TAB}")
End If
End Sub
<小时>
基本上在此事件期间,我无法看到输入时单元格将是"的值.
Basically during this event I'm not able to see what the value of the cell "will be" when entered.
我尝试添加.RefreshEdit"和.Commit",但没有成功.
I tried adding a ".RefreshEdit" and a ".Commit" but they didn't work.
有什么方法可以测试此事件中的代码,或者是否有一个事件会在我可以使用之后立即触发?
Any way to test the code within this event OR is there an event that would fire IMMEDIATELY afterward that I can use?
推荐答案
您找错地方了.您需要检查 TextBox 中的文本,而不是网格,以查看当前正在键入的字符数.尝试使用 TextChanged 事件:
You are looking in the wrong place. You need to examine the text in the TextBox, not the grid, to see how many characters are currently being typed. Try using the TextChanged event for that:
Private Sub txtdgvDiaryEdit_TextChanged(sender As Object, e As EventArgs)
If DirectCast(sender, TextBox).Text.Length = 2 Then
SendKeys.Send("{TAB}")
End If
End Sub
像您的其他代码一样,添加处理程序:
Like your other code, add the handlers:
'remove any existing handler
RemoveHandler txtEdit.TextChanged, AddressOf txtdgvDiaryEdit_TextChanged
AddHandler txtEdit.TextChanged, AddressOf txtdgvDiaryEdit_TextChanged
RemoveHandler txtEdit.KeyPress, AddressOf txtdgvDiaryEdit_KeyPress
AddHandler txtEdit.KeyPress, AddressOf txtdgvDiaryEdit_KeyPress
或者,您可以检查 TextBox 是否只有一个字符,如果 KeyPress 传入另一个数字,则发送您的 Tab 键.在这种情况下,您将删除 TextChanged 事件代码:
Alternatively, you can check to see if the TextBox only has one character, and if the KeyPress is passing in another number, send your Tab key then. You would remove the TextChanged event code in this case:
Private Sub txtdgvDiaryEdit_KeyPress(sender As Object, e As KeyPressEventArgs)
'Test for numeric value or backspace
If IsNumeric(e.KeyChar.ToString()) _
Or e.KeyChar = ChrW(Keys.Back) Then
e.Handled = False 'if numeric
Else
e.Handled = True 'if non numeric
End If
If DirectCast(sender, TextBox).Text.Length = 1 AndAlso Char.IsNumber(e.KeyChar) Then
SendKeys.Send("{TAB}")
End If
End Sub
这篇关于VB.NET 2010 DataGridView 通过 EditingControlShowing 事件处理按键的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!