【问题标题】:how to check whether a number or letter has been entered in a textbox on KeyUp Event [duplicate]如何检查是否在 KeyUp 事件的文本框中输入了数字或字母 [重复]
【发布时间】:2021-02-11 15:11:37
【问题描述】:

在 KeyPress 或 KeyUp 事件中,我该如何做这样的事情:

if (String.IsOnlyANumber(textBox.Text))
{ 
    do my things
}
else if(String.IsLettersAndEverythingElse(textBox.Text))
{
    do my other things
}

【问题讨论】:

标签: c# winforms events textbox


【解决方案1】:
private void your_textBox_KeyPress(object sender, KeyPressEventArgs e)
{
    if (char.IsDigit(e.KeyChar)) 
    {
        // is a number
    }
    else 
    {
        // is anything else
    }
}

【讨论】:

    【解决方案2】:

    试试这个,

    textBox1.Text = string.Concat(textBox1.Text.Where(char.IsLetterOrDigit));
    

    在您的 KeyPress 或 KeyUp 事件中。

    【讨论】:

      猜你喜欢
      • 2015-12-17
      • 1970-01-01
      • 2014-07-01
      • 2013-06-20
      • 2011-03-06
      • 2017-07-21
      • 2020-04-30
      • 1970-01-01
      • 2016-02-10
      相关资源
      最近更新 更多