我是 winforms 新手..我正在尝试将 DataGridView 的两列设置为仅数字..我不希望用户能够在单元格中键入任何内容,除非它在一列中是自然数,而在另一列中是数值(始终是一位小数)。
我认为这很简单..但即使在尝试了 stackoverflow 和其他网站上的很多东西之后我仍然无法实现这一点。
If DataGridView1.CurrentCell.ColumnIndex = 8 Then
If Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsDigit(e.KeyChar) AndAlso e.KeyChar <> "."c Then
e.Handled = True
End If
End If
试试这个代码
Private Sub DataGridView1_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles DataGridView1.EditingControlShowing
If DataGridView1.CurrentCell.ColumnIndex = 2 Then
AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress
ElseIf DataGridView1.CurrentCell.ColumnIndex = 1 Then
AddHandler CType(e.Control, TextBox).KeyPress, AddressOf TextBox_keyPress1
End If
End Sub
Private Sub TextBox_keyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If Char.IsDigit(CChar(CStr(e.KeyChar))) = False Then e.Handled = True
End Sub
Private Sub TextBox_keyPress1(ByVal sender As Object, ByVal e As KeyPressEventArgs)
If Not (Char.IsDigit(CChar(CStr(e.KeyChar))) Or e.KeyChar = ".") Then e.Handled = True
End Sub
仅数字的文本框按键事件
TextBox_keyPress1 具有十进制值的数字事件
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)