16,554
社区成员
发帖
与我相关
我的任务
分享
Namespace WindowsFormsApplication
Public NotInheritable Class MyDataGridView
Inherits DataGridView
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = Keys.Enter Then
Me.OnKeyPress(New KeyPressEventArgs("r"c))
' 下面是对的()
' SendKeys.Send("{r}")
Return True
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function
End Class
End Namespace
Private Sub myDataGridView1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If e.KeyChar = "r"c Then
Dim dgv As DataGridView = TryCast(sender, DataGridView)
Dim cell As DataGridViewCell = dgv.CurrentCell
If cell.IsInEditMode Then
'限制单元格只能输入test
If cell.EditedFormattedValue IsNot Nothing AndAlso cell.EditedFormattedValue.ToString() <> "test" Then
MessageBox.Show("输入内容不合格")
Else
dgv.CurrentCell = dgv(cell.ColumnIndex, cell.RowIndex + 1)
End If
End If
End If
End Sub
Private Sub myDataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs)
Dim dgv As DataGridView = TryCast(sender, DataGridView)
Dim cell As DataGridViewCell = dgv(e.ColumnIndex, e.RowIndex)
If True Then
'限制单元格只能输入test
If cell.EditedFormattedValue IsNot Nothing AndAlso cell.EditedFormattedValue.ToString() <> "test" Then
MessageBox.Show("输入内容不合格")
dgv.CurrentCell = cell
Else
dgv.CurrentCell = dgv(cell.ColumnIndex, cell.RowIndex + 1)
End If
End If
End Sub
Protected Overrides Function ProcessCmdKey(ByRef msg As System.Windows.Forms.Message, keyData As System.Windows.Forms.Keys) As Boolean
If keyData = System.Windows.Forms.Keys.Enter Then
End If
Return MyBase.ProcessCmdKey(msg, keyData)
End Function
Private Sub dataGridView1_EditingControlShowing(sender As Object, e As DataGridViewEditingControlShowingEventArgs)
cotrol = New TextBox()
cotrol = DirectCast(e.Control, TextBox)
cotrol.KeyPress += New KeyPressEventHandler(AddressOf cotrol_KeyPress)
End Sub
Private Sub cotrol_KeyPress(sender As Object, e As KeyPressEventArgs)
End Sub
Public Class Form1
Private Sub myDataGridView1_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs) Handles dg.KeyPress
If e.KeyChar = "r"c Then
Dim dgv As DataGridView = TryCast(sender, DataGridView)
Dim cell As DataGridViewCell = dgv.CurrentCell
If cell.IsInEditMode Then
'限制单元格只能输入test
If cell.EditedFormattedValue IsNot Nothing AndAlso cell.EditedFormattedValue.ToString() <> "test" Then
MessageBox.Show("输入内容不合格")
Else
dgv.CurrentCell = dgv(cell.ColumnIndex, cell.RowIndex + 1)
End If
End If
End If
End Sub
Private Sub myDataGridView1_CellEndEdit(ByVal sender As Object, ByVal e As DataGridViewCellEventArgs) Handles dg.CellEndEdit
Dim dgv As DataGridView = TryCast(sender, DataGridView)
Dim cell As DataGridViewCell = dgv(e.ColumnIndex, e.RowIndex)
If True Then
'限制单元格只能输入test
If cell.EditedFormattedValue IsNot Nothing AndAlso cell.EditedFormattedValue.ToString() <> "test" Then
MessageBox.Show("输入内容不合格")
dgv.CurrentCell = cell
Else
dgv.CurrentCell = dgv(cell.ColumnIndex, cell.RowIndex + 1)
End If
End If
End Sub
Dim WithEvents dg As New WindowsFormsApplication.MyDataGridView
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
dg.AllowUserToAddRows = True
dg.Columns.Add("id", "123")
dg.Rows.Add(3)
Me.Controls.Add(dg)
End Sub
End Class
Namespace WindowsFormsApplication
Public NotInheritable Class MyDataGridView
Inherits DataGridView
Protected Overrides Function ProcessCmdKey(ByRef msg As Message, ByVal keyData As Keys) As Boolean
If keyData = Keys.Enter Then
Me.OnKeyPress(New KeyPressEventArgs("r"c))
' 下面是对的()
' SendKeys.Send("{r}")
Return True
Else
Return MyBase.ProcessCmdKey(msg, keyData)
End If
End Function
End Class
End Namespace
代码可以正常运行。
因为myDataGridView1_KeyPress方法没有和KeyPress事件绑定,所以不会接收到任何按键的