我有一个自定义DataGridViewCell
,其中包含一个用户控件作为其编辑控件。该单元格是以本文的方式构建的:http://msdn.microsoft.com/en-us/library/system.windows.forms.datagridviewcell.initializeeditingcontrol.aspx
在大多数情况下,这很有效。问题是DataGridViewCell
的常规行为是将用于输入EditMode的击键传递给控件,但是这个单元格没有发生。
示例:
我选择自定义日期单元格,输入“12/24/2008”,“1”开始编辑模式,单元格中出现“2/24/2008”。
如何进行编辑控件的第一次击键?
Public Class DataGridViewDateCell
Public Overrides Function KeyEntersEditMode(ByVal e As System.Windows.Forms.KeyEventArgs) As Boolean
'Any key-sequences with modifiers will not enter edit mode'
If e.Control Or e.Shift Or e.Alt Then Return False
'Accept numbers, '/', and '-''
If (e.KeyCode >= Keys.NumPad0 And e.KeyCode <= Keys.NumPad9) OrElse _
(Char.IsDigit(ChrW(e.KeyCode))) OrElse _
e.KeyCode = Keys.Subtract OrElse _
e.KeyCode = Keys.Divide OrElse _
e.KeyCode = Keys.OemMinus OrElse _
e.KeyCode = Keys.OemQuestion Then
Return True
End If
'Any other keys should be ignored'
Return False
End Function
Public Overrides ReadOnly Property EditType() As System.Type
Get
Return GetType(DataGridViewDateEditingControl)
End Get
End Property
End Class
答案 0 :(得分:1)
我明白了!
自定义控件上有一个TextBox
始终具有焦点(至少在usercontrol中)。当用户键入时,键击将直接转到TextBox
。但是当DataGridViewCell
将键击传递给控件时,它会转到用户控件,而不是 TextBox
。
我将此代码添加到usercontrol以修复它:
Private Sub ucDateTime_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles MyBase.KeyPress
DateText.AppendText(e.KeyChar)
DateText_KeyPress(sender, e)
End Sub
答案 1 :(得分:0)
在表单中捕捉击键:
Private Sub Form1_KeyDown(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles Me.KeyDown
If MyGrid.Focused AndAlso MyGrid.CurrentRow IsNot Nothing Then
MyGrid.CurrentCell = MyGrid(4, MyGrid.CurrentRow.Index)
MyGrid.BeginEdit(True)
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.KeyPreview = True
End Sub