请教 如何使在textbox中只能粘贴数字

解决方案 »

  1.   

    在 TextChanged 中进行判断string s = "";foreach(char c in this.textBox1.Text.ToCharArray())
    {
    if(c >= '0' && c <= '9') s += c.ToString();
    }this.textBox1.Text = s;
      

  2.   

    private void InitializeComponent()
    {
    this.textBox1.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.textBox1_KeyPress);
    }private void textBox1_KeyPress(object sender, System.Windows.Forms.KeyPressEventArgs e)
    {
    if(e.KeyChar>'9' || e.KeyChar<'0') e.Handled=true;
    }