以下是让TextCell只处理数字键的代码,希望对您有所帮助,如果需要更精细的控制(如负号只能出现在第一个字符的位置),请修改Control_KeyPress函数中的逻辑。
C# Code
- private void Form1_Load(object sender, EventArgs e)
- {
- this.gcMultiRow.EditingControlShowing += new EventHandler<EditingControlShowingEventArgs>(gcMultiRow_EditingControlShowing);
- }
- void gcMultiRow_EditingControlShowing(object sender, EditingControlShowingEventArgs e)
- {
- //if (gcMultiRow.CurrentCell.Name != "TextBoxCell1")
- // 不是需要处理的Cell
- //{
- // return;
- //}
- e.Control.KeyPress -= new KeyPressEventHandler(Control_KeyPress);
- e.Control.KeyPress += new KeyPressEventHandler(Control_KeyPress);
- }
- void Control_KeyPress(object sender, KeyPressEventArgs e)
- {
- if (!char.IsNumber(e.KeyChar) &&
- e.KeyChar != '\b' &&
- e.KeyChar != '-' &&
- e.KeyChar != ',')
- {
- e.Handled = true;
- }
- }
复制代码 VB Code
- Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
- AddHandler gcMultiRow.EditingControlShowing, AddressOf gcMultiRow_EditingControlShowing
- End Sub
-
- Private Sub gcMultiRow_EditingControlShowing(ByVal sender As Object, ByVal e As EditingControlShowingEventArgs)
- 'If gcMultiRow.CurrentCell.Name <> "TextBoxCell1" Then
- ' ' 不是需要处理的Cell
- ' Return
- 'End If
- RemoveHandler e.Control.KeyPress, AddressOf Control_KeyPress
- AddHandler e.Control.KeyPress, AddressOf Control_KeyPress
- End Sub
- Private Sub Control_KeyPress(ByVal sender As Object, ByVal e As KeyPressEventArgs)
- If Not Char.IsNumber(e.KeyChar) AndAlso
- e.KeyChar <> ControlChars.Back AndAlso
- e.KeyChar <> "-"c AndAlso
- e.KeyChar <> ","c Then
- e.Handled = True
- End If
- End Sub
复制代码 |