DataGridView Keypress事件只能输入数字

#Region "------- 处理 Grid 中的 Keypress ,以限定某些字段仅可输入数字等 -------- " ' 各Grid 中的 Seq No. 为 Int 类型; 其中的 Days 为 Int 类型; 其它的 Cost / Expense 为 Decimal 类型 Private EditingControl As DataGridViewTextBoxEditingControl ' ' ' <summary> ' ' ' 限定仅可以输入数字值及相关的操作符,如 ". "/回车/退格/删除等键 ' ' ' </summary> ' ' ' <remarks> 不可输入小数点 </remarks> Private Sub editingcontrol_INT_Keypress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Dim keyAscii As Integer = Asc(e.KeyChar) If (keyAscii < Keys.D0) Or keyAscii > Keys.D9 Then _ If keyAscii <> Keys.Enter And keyAscii <> Keys.Back Then e.Handled = True End Sub ' ' ' <summary> ' ' ' 限定仅可以输入数字值及相关的操作符,如 ". "/回车/退格/删除等键 ' ' ' </summary> ' ' ' <remarks> 可输入小数点 </remarks> Private Sub editingcontrol_Decimal_Keypress(ByVal sender As System.Object, ByVal e As KeyPressEventArgs) Dim keyAscii As Integer = Asc(e.KeyChar) If (keyAscii < Keys.D0 And keyAscii <> Keys.Delete) Or keyAscii > Keys.D9 Then _ If keyAscii <> Keys.Enter And keyAscii <> Keys.Back Then e.Handled = True End Sub Private Sub grdALL_EditingControlShowing(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewEditingControlShowingEventArgs) Handles grdRoute.EditingControlShowing, grdHotel.EditingControlShowing, grdVisaInfo.EditingControlShowing, grdRouteInfo.EditingControlShowing EditingControl = CType(e.Control, DataGridViewTextBoxEditingControl) Select Case CType(sender, DataGridView).CurrentCell.OwningColumn.Name.Trim.ToLower Case "seq_no ", "booking_days " ' int AddHandler EditingControl.KeyPress, AddressOf editingcontrol_INT_Keypress Case "estimated_cost " ' Decimal AddHandler EditingControl.KeyPress, AddressOf editingcontrol_Decimal_Keypress End Select End Sub Private Sub grdALL_CellEndEdit(ByVal sender As Object, ByVal e As System.Windows.Forms.DataGridViewCellEventArgs) Handles grdRoute.CellEndEdit, grdHotel.CellEndEdit, grdVisaInfo.CellEndEdit, grdRouteInfo.CellEndEdit Select Case CType(sender, DataGridView).CurrentCell.OwningColumn.Name.Trim.ToLower Case "seq_no ", "booking_days " ' int RemoveHandler EditingControl.KeyPress, AddressOf editingcontrol_INT_Keypress Case "estimated_cost " ' Decimal RemoveHandler EditingControl.KeyPress, AddressOf editingcontrol_Decimal_Keypress End Select 'RemoveHandler EditingControl.KeyPress, AddressOf editingcontrol_INT_Keypress 'RemoveHandler EditingControl.KeyPress, AddressOf editingcontrol_Decimal_Keypress End Sub #End Region

你可能感兴趣的:(object,Integer)