【问题标题】:is there a code that allows the textbox input in numbers?是否有允许文本框输入数字的代码?
【发布时间】:2021-12-26 18:42:06
【问题描述】:

我在 C# windows 窗体中创建了一个航空公司管理程序,我有一个注册表单,有两个字段(文本框)我想以数字形式取值。

Textbox1(电话号码) Textbox2(国民身份证号码)

我希望这些文本框分别接受 11 位和 16 位数字的输入。

虽然我希望文本框使用 (-) 破折号以受限方式进行输入,例如

####-#######(用于Textbox1)

#####-#######-#(用于Textbox2)

还有,代码应该放在哪个块中?

【问题讨论】:

  • 你在找MaskedTextBox控件吗?
  • 也许我不确定,这是我第一次使用 C#
  • 同意德米特里;这是 MaskedTextBox 的工作。在这里阅读:docs.microsoft.com/en-us/dotnet/api/…

标签: c# winforms


【解决方案1】:

我根据您的示例为 Textbox1 和 Textbox2 设置了“MaxLength”:

####-#######(对于 Textbox1)'MaxLength'=12

#####-#######-#(对于Textbox2)'MaxLength'=15

    private void textBox1_KeyDown(object sender, KeyEventArgs e)
    {
        //Switch is faster than if
        switch (!((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) || e.KeyCode == Keys.Back || e.KeyCode == Keys.Home || e.KeyCode == Keys.End || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
        {
            case true:
                e.SuppressKeyPress = true;//Non-numeric characters are not written with this command
                break;
            default:
                switch (e.KeyCode != Keys.Back && e.KeyCode != Keys.Home && e.KeyCode != Keys.End && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right)
                {
                    case true:
                        //This is how the total number of separators is calculated
                        byte j = 0;
                        for (byte i = 0; i < textBox1.Text.Length; i++)
                        {
                            switch (textBox1.Text[i] == '-')
                            {
                                case true:
                                    j++;
                                    break;
                            }
                        }
                        //You can change this section, based on your country's national identification code or phone number
                        switch (j < 5)//Check total separators
                        {
                            case true:
                                switch (textBox1.Text.Length)
                                {
                                    case 4:
                                        textBox1.Text = textBox1.Text + '-';
                                        textBox1.Select(textBox1.TextLength, textBox1.TextLength);
                                        break;
                                }
                                break;
                        }
                        break;
                }
                break;
        }
    }

    private void textBox2_KeyDown(object sender, KeyEventArgs e)
    {
        //Switch is faster than if
        switch (!((e.KeyCode >= Keys.D0 && e.KeyCode <= Keys.D9) || (e.KeyCode >= Keys.NumPad0 && e.KeyCode <= Keys.NumPad9) || e.KeyCode == Keys.Back || e.KeyCode == Keys.Home || e.KeyCode == Keys.End || e.KeyCode == Keys.Left || e.KeyCode == Keys.Right))
        {
            case true:
                e.SuppressKeyPress = true;//Non-numeric characters are not written with this command
                break;
            default:
                switch (e.KeyCode != Keys.Back && e.KeyCode != Keys.Home && e.KeyCode != Keys.End && e.KeyCode != Keys.Left && e.KeyCode != Keys.Right)
                {
                    case true:
                        //This is how the total number of separators is calculated
                        byte j = 0;
                        for (byte i = 0; i < textBox2.Text.Length; i++)
                        {
                            switch (textBox2.Text[i] == '-')
                            {
                                case true:
                                    j++;
                                    break;
                            }
                        }
                        //You can change this section, based on your country's national identification code or phone number
                        switch (j < 14)//Check total separators
                        {
                            case true:
                                switch (textBox2.Text.Length)
                                {
                                    case 5:
                                        textBox2.Text = textBox2.Text + '-';
                                        textBox2.Select(textBox2.TextLength, textBox2.TextLength);
                                        break;
                                    case 13:
                                        textBox2.Text = textBox2.Text + '-';
                                        textBox2.Select(textBox2.TextLength, textBox2.TextLength);
                                        break;
                                }
                                break;
                        }
                        break;
                }
                break;
        }
    }

输出:

测试于:

Visual Studio 2017、.NET Framework 4.5.2、Windows 窗体

谢谢

【讨论】:

  • 注意:如果这个解决方案对你有帮助,请标记为答案(别忘了给我投票)
  • 如果您所在国家/地区的语言是从右到左的,请告诉我以更新解决方案。
  • 请说明您是如何解决这个问题的,而不仅仅是发布代码。纯代码的答案通常没那么有用。
  • 我不得不说 switch 语句很可怕,switch 并不比使用少于 5 个 case 快。更不用说增加的复杂性了。
  • 嗯,不,不存在通过复杂性实现安全性的东西。
猜你喜欢
  • 2018-02-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-04-12
  • 1970-01-01
  • 2014-11-16
  • 1970-01-01
  • 2010-12-16
相关资源
最近更新 更多