、只允许输入数字
控制文本框中只能输入数字主要是通过TextBox控件的KeyPress事件实现的。KeyPress事件用来在控件有焦点的情况下,按下键时发生,语法为: public event KeyPressEventHandler KeyPress KeyPressEventHandler表示将要处理Control的KeyPress事件的方法。其语法为: public delegate void KeyPressEventHandler(object sender,KeyPressEventArgs e) 参数说明: sender:事件源; e :包含事件数据的KeyPressEventArgs 。 KeyPressEventArgs对象有Handled属性和KeyChar属性。 (1)Handled属性: 获取或设置一个值,指示是否处理过KeyPress事件。如果处理过,则为True,否则为False。 (2)KeyChar属性: 获取或设置与按下的键对应的字符。属性值为键盘对应的ASCII字符。 eg. 判断用户是否按下回车键,代码如下: private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 常用键盘对应的ASCII字符集: 常数 值 描述 0——9的ASCII值对应分别为48——57 回车:13 换行:10; 空:0; 退格(Backspace):8; 因此:只允许输入数字的程序代码为:(注意:在限制用户输入非0~9间数字的同时,不应限制用户输入“回车”和“退格”) private void txtSum_KeyPress(object sender, KeyPressEventArgs e) 2、限制输入长度及设置密码文本 (1)限制输入长度: 可通过设置TextBox控件的MaxLength属性即可限制最大长度。也可以在Form_Load函数中限制,如: private void Form1_Load(object sender, EventArgs e) 如果输入字符超过最大长度需要输入提示语言,示例代码为: private void textBox1_KeyPress(object sender, KeyPressEventArgs e) 限制的最小长度一般需要在信息提交(如点击确定键)时确定,如: private void button1_Click(object sender, EventArgs e) (2)设置密码文本: 当输入密码时需要显示为“*”或“#”时,只需要设置控件的PasswordChar为“*”或“#”即可。 也可以在Form_load函数中设置:this.textBox2.PasswordChar = '*'; 3、自动删除非法字符 主要通过KeyUp事件,KeyValue属性(对应键盘值的ASCII码),Select()方法(选取文本框的文本范围,有两个参数:Start和length),SelectAll()方法,SelectedText属性等实现。 如:实现只输入1~8,其他字符则自动删除的功能: private void textBox1_KeyUp(object sender, KeyEventArgs e) textBox1.Select(0, textBox1.Text.Length - 1 ); //设置文本框中选中的文本 也可以通过输入非法字符时忽略该输入实现: private void textBox1_KeyPress(object sender, KeyPressEventArgs e) e.Handled = true; 4、带记忆功能的文本框(为TextBox控件中添加列表选择框) 双击文本框显示以前输入过的项: (1)、双击实现(可设置两次单击鼠标按钮之间的时间,以确定是否为双击事件) private void textBox1_DoubleClick(object sender, EventArgs e) (2)、KeyPress事件 private void textBox1_KeyPress(object sender, KeyPressEventArgs e) (3)、当点击listBox中的项时,在textBox中显示该文本 private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if ((e.KeyChar ==13)
{
MessageBox.Show("您按下了回车键");
}
}
vbKeyA 65 A 键
vbKeyB 66 B 键
vbKeyC 67 C 键
vbKeyD 68 D 键
vbKeyE 69 E 键
vbKeyF 70 F 键
vbKeyG 71 G 键
vbKeyH 72 H 键
vbKeyI 73 I 键
vbKeyJ 74 J 键
vbKeyK 75 K 键
vbKeyL 76 L 键
vbKeyM 77 M 键
vbKeyN 78 N 键
vbKeyO 79 O 键
vbKeyP 80 P 键
vbKeyQ 81 Q 键
vbKeyR 82 R 键
vbKeyS 83 S 键
vbKeyT 84 T 键
vbKeyU 85 U 键
vbKeyV 86 V 键
vbKeyW 87 W 键
vbKeyX 88 X 键
vbKeyY 89 Y 键
vbKeyZ 90 Z 键
小写字母为对应大写字母加上32
{
if ((e.KeyChar != 8 && !char.IsDigit(e.KeyChar))&&e.KeyChar!=13)
{
MessageBox.Show("只能输入数字","操作提示",MessageBoxButtons.OK,MessageBoxIcon.Information);
e.Handled = true;
}
}
{
this.textBox1.MaxLength = 6;
}
{
if (textBox1.Text.Length == 6)
{
MessageBox.Show("用户名只能输入6个字符");
}
}
{
if (textBox2.Text.Length < 6)
{
MessageBox.Show("用户密码不能低于6位");
this.textBox2.Text = "";
textBox2.Focus();
}
}
{
if (e.KeyValue <= 48 || e.KeyValue >= 57)
{
textBox1.Text = textBox1.SelectedText;
textBox1.SelectAll(); //选择全部文本
textBox1.SelectionStart = textBox1.Text.Length;
textBox1.Focus(); //获得焦点
e.Handled = true;
}
}
{
if (!char.IsDigit(e.KeyChar))
{
}
}
{
if (listBox1.Items.Count != 0)
{
this.listBox1.Visible = true;
}
}
{
if (e.KeyChar == 13)
{
if (textBox1.Text.ToString() != "")
{
this.listBox1.Items.Add(this.textBox1.Text);
this.textBox1.Text = "";
}
}
}
{
if (listBox1.SelectedItem.ToString() != "")
{
textBox1.Text = listBox1.SelectedItem.ToString();
textBox1.SelectionStart = this.textBox1.Text.Length;
textBox1.Focus();
listBox1.Visible = false;
}
}
C# 程序开发范例宝典读书笔记(七)——TextBox控件
最新推荐文章于 2024-08-22 15:21:18 发布