想在richTextBox中只接受0-9的数字,及负号输入,以回车结束,其余字符不接受。设置richTextBox_KeyDown()可以实现吗?请问怎样用代码实现啊?谢谢!

解决方案 »

  1.   

        Private Sub TextBoxSend_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles TextBoxSend.KeyPress        If CheckBoxHexSend.Checked = True Then
                '文本框防止非法字符输入:
                '只输入整数和A-F:及 空格
                '3 - ctrl + c 复制
                '22 - ctrl + v 粘贴
                '24 - ctrl + x 剪切
                '13  回车键
                '17  控制键,允许复制
                '8   退格键
                '32  空格键
                Dim KeyAscii As Short = Asc(e.KeyChar)            Select Case KeyAscii
                    Case 13, 32, 8, Asc("0") To Asc("9"), Asc("A") To Asc("F"), Asc("a") To Asc("f")    '加回车键
                        ' e.KeyChar = Chr(KeyAscii)
                    Case Else
                        KeyAscii = 0 '按键都屏蔽
                End Select
                If KeyAscii = 0 Then
                    e.Handled = True
                End If        End If    End Sub‘类似这样改吧,已经说得很清楚啦
      

  2.   


                if (47 < e.KeyValue && e.KeyValue < 58)
                {
                    if (e.Shift)
                    {
                        if(e.KeyCode==Keys.D2)
                            KeyPressValue += "@";
                    }
                    else
                        KeyPressValue += e.KeyCode.ToString().Replace("D", "");
                }判断键入的键盘Value值,数字键0-9对应的ascii码是48-57
      

  3.   


    http://www.developerfusion.com/tools/convert/vb-to-csharp/
    这是改成C#的网站
      

  4.   

    用keypress事件就处理。如果还要实现其他功能,有可能还会用到KeyDown事件。
      

  5.   


    private void TextBoxSend_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if (CheckBoxHexSend.Checked == true) {
    //文本框防止非法字符输入:
    //只输入整数和A-F:及 空格
    short KeyAscii = Strings.Asc(e.KeyChar);
    switch (KeyAscii) {
    case 13:
    case 32:
    case 8:
    case Strings.Asc("0"): // TODO: to Strings.Asc("9")
    case Strings.Asc("A"): // TODO: to Strings.Asc("F")
    case Strings.Asc("a"): // TODO: to Strings.Asc("f")
    //加回车键
    break;
    // e.KeyChar = Chr(KeyAscii)
    default:
    KeyAscii = 0;
    //按键都屏蔽
    break;
    }
    if (KeyAscii == 0) {
    e.Handled = true;
    }
    }
    }
    //类似这样改吧,已经说得很清楚啦--不好意思新手练习发源代码-实验下
      

  6.   

    用ajax,很容易实现
    <ajaxToolkit:FilteredTextBoxExtender ID="ftbe" runat="server" TargetControlID="TextBox" FilterType="Custom, Numbers" ValidChars="+-*/" /> 
      

  7.   


            /// <summary>
            /// 判断是否为纯数字
            /// </summary>
            /// <param name="txt"></param>
            /// <returns></returns>
            public static bool isNumornot(string txt)
            {
                if (Regex.Match(txt, "^[0-9]+$").Success)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
      

  8.   

    在控件中定义事件:
    onKeyUp="this.value=this.value.replace(/\D/g,'')" onafterpaste="this.value=this.value.replace(/\D/g,'')"