键盘操作textBox1,无论是退格键backspace,还是删除键delete,操作之后 textBox1.Text = "1",该如何实现呢?谢谢

解决方案 »

  1.   

    private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
            {
                if (e.KeyChar == (char)8 || e.KeyChar == (char)46)
                {
                    this.textBox1.Text = "1";
                }
            }
      

  2.   


      public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
                textBox1.TextChanged+=new EventHandler(textBox1_TextChanged);
            }        private void textBox1_TextChanged(object sender, EventArgs e)
            {
                if (textBox1.Text != "1")
                {
                    textBox1.Text = "1";
                }
            }
        }
      

  3.   

    你可以查,可以测试一下,按哪个键的时候,它对应的 KeyChar,让MessageBox显示其值出来...
      

  4.   


    使用delete键,依然可以是的textBox1成为空白,希望的效果是 textBox1,永远不能成为空白
      

  5.   

    textBox1_Changed
    {
        if (textBox1.Text == "") textBox1.Text == "1";
    }
      

  6.   

    搜索消息钩子,或者简单一点如下:
     private void textBox1_KeyDown(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == (Keys.RButton | Keys.MButton | Keys.Back | Keys.Space) || e.KeyCode == Keys.Back)
                {
                    this.textBox1.Text = "1111111";
                }
            }
      

  7.   

    你可以使用钩子来实现的,捕获键盘事件,或者直接textchange事件来做,