在KeyPress事件中做处理,如果输入的您不允许输入的内容,
e.Handled=false;
这样就可以了。我可以给你一段我做过的一个只能输入数值的TextBox控件的部分代码,原理和这个相似private void NumTextBox_KeyPress(object sender, KeyPressEventArgs e)
{
int KeyAscii;
KeyAscii = (int)e.KeyChar;
// 0 - 9 ,退格,回车
if ((KeyAscii >= 48 && KeyAscii <= 57) || KeyAscii == 8 || KeyAscii == 13)
{ }
else if(KeyAscii == 45) // -  负号的处理
{

if (this.Text.IndexOf("-") >= 0) { KeyAscii = 0; }
if (this.SelectionStart != 0) { KeyAscii =0; }
}
else if (KeyAscii == 46) // . 小数点的处理

if (this.Text.IndexOf(".") >= 0)
KeyAscii = 0; 
}
else{ KeyAscii = 0; } if (KeyAscii==0)
e.Handled = true;
else
e.Handled = false;
}

解决方案 »

  1.   

    简单
    private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    string s="一二三四五六七八九十零"; 
    if(s.IndexOf(e.KeyChar)==-1)
    {
    e.Handled=true;
    this.Focus();//如果是form是MDI的子窗体,将this改成MDI窗体实例 
    }
    }
      

  2.   

    '//有点思路,一定会有更简单的方法,比如正则也许可以,可是我不会,只能给人一个vb.net加自然语言的版本了,你调调吧,也许会给你一些提醒
    frm_load()
    '在frmload时写
    old=textbox2.text
    end sub
    Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChangedif not (textbox2.text.replace(oldstr,"") in "一二三四")
         textbox2.text=oldstr
    end ifoldstr=textbox2.text
    End Sub
      

  3.   

    to CMIC(大象)你的方法在Form中是可行的但是如果是用在一个用户自定义控件中该怎么办呢?
      

  4.   

    另外 this.Focus(); 到底起的什么作用?
      

  5.   

    OK this.Parent.Focus(); 好像也可以,但是为什么要 Focus啊?
      

  6.   

    当输入法打开时form会失去焦点,e.Handled=true不起作用。
      

  7.   

    原来如此但是为什么要把焦点设置到上一层的控件/Form?我试了一下,this.Focus好像没有什么用处
      

  8.   


    Key_Press事件和Key_Changed()事件都 可以。
      

  9.   

    control可以用
    Focus获得输入焦点
    KeyPressEventArgs.Handled
    来标释是否处理KeyPress事件
      

  10.   

    protected override void OnTextChanged(System.EventArgs e)
    {
    string str = "";
    foreach(char c in this.Text)
    {
    if(Char.IsDigit(c))
    {
    str += c.ToString();
    }
    }
    this.Text = str;
    }
    protected override void OnKeyDown(KeyEventArgs e)
    {
    if((e.KeyCode ==Keys.V)&&(e.Control==true))
    {
    e.Handled = true;
    }
    else
    {
    base.OnKeyDown(e);
    }
    }
    protected override void OnMouseDown(MouseEventArgs e)
    {
    if(e.Button !=MouseButtons.Right)
    {
    base.OnMouseDown(e);
    }
    }
    }
    你看看也许对你有用!
      

  11.   

    String str = "一二三四五六七八九十零";
      

  12.   

    win form 的可以。
    web的怎么办?