VB.NET 2010 DataGridView Handling Keypress via EditingControlShowing Event

梦想与她 提交于 2021-01-28 21:55:19

问题


I am working with a DataGridView for the first time and while I have MANY questions, this latest issue is vexing me.

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) 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.

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?


回答1:


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

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


来源:https://stackoverflow.com/questions/31172081/vb-net-2010-datagridview-handling-keypress-via-editingcontrolshowing-event

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!