如何使文本框不乱扔垃圾 [英] How to make the textbox Does not accept litter

查看:82
本文介绍了如何使文本框不乱扔垃圾的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使文本框不接受垃圾

How to make the textbox Does not accept litter

推荐答案

如果您的意思是不接受字母",例如"A"或"B"或"C",然后:
处理TextBox KeyPress事件:
If you mean "not accept letter" as in ''A'' or ''B'' or ''C'', then:
handle the TextBox KeyPress event:
private void myTextBox_KeyPress(object sender, KeyPressEventArgs e)
    {
    if (char.IsLetter(e.KeyChar))
        {
        e.Handled = true;
        }
    }


如果您是指文本框不接受字母或不可编辑并且您使用的是Forms,请查看
If you mean that the text box doesn''t accept a letter or isn''t editable and you use Forms, have a look at ReadOnly[^] property


您可以还使用正则表达式来验证TextChanged事件上的文本框文本.

You can also use regular expressions to validate the textbox text on the TextChanged event.

private void textBox1_TextChanged(object sender, EventArgs e)
{
    Match myMatch = Regex.Match(textBox1.Text.ToLower(), "[a-z]");
    if (myMatch.Success)
    {
        MessageBox.Show("No letters allowed...");
        textBox1.Text = textBox1.Text.Remove(textBox1.Text.Length - 1);
    }
}


这篇关于如何使文本框不乱扔垃圾的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆