e.Handled 的应用,控制textBox所输入内容

来源:互联网 发布:sql 修改表字段默认值 编辑:程序博客网 时间:2024/05/18 06:45

 

           Handled     获取或设置一个值,该值指示是否处理过   KeyPress   事件   

           e.Handled    true;//为true时表示已经处理了事件(即不处理当前键盘事件)
           e.Handled为false的时候表示可以接受该事件
           KeyChar     获取或设置与按下的键对应的字符
1. 设置textBox只可输入数字,并识别退格键

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)        {            if (e.KeyChar != 8 && !char.IsDigit(e.KeyChar))//如果不是退格键和数字            { e.Handled = true; }        }

2.设置textBox不识别空格键:

        private void textBox25_KeyPress(object sender, KeyPressEventArgs e)        {            if (e.KeyChar == ' ') e.Handled = true;        }

textBox输入两个数字后,自动跳转到下一个textBox表格并选中其所有内容

private void NUM_TextChanged(object sender, EventArgs e)        {            if (((TextBox)sender).SelectionLength > 0) return;              if (((TextBox)sender).Text.Length >= 2)            {                SelectNextControl((Control)sender, true, true, true, true);                ((TextBox)ActiveControl).SelectAll();             }        }



 

原创粉丝点击