【问题标题】:Format c# textbox to only allow numeric characters [duplicate]格式化c#文本框只允许数字字符[重复]
【发布时间】:2014-04-08 08:09:02
【问题描述】:

我一直在研究here和dotnetperls的解决方案,试图找到一种方法来格式化我的文本框以仅允许数字字符。我要么只是不明白,要么真的不明白代码来做我想做的事。

我有一个表单,它会根据在另一个表单中按下的按钮而改变,我需要年龄框只允许最多 99 个数字字符(因此用户可以输入 1-99 范围内的年龄。我'已经研究过正则表达式方法和屏蔽文本框,但就是不明白。

有人能指出我将用来实现这一目标的方向吗?

我的代码如下:

private void AddItem_Load(object sender, EventArgs e)
    {
        if (formName == "customer")
        {
            this.Text = "Add Customer";
            lblZero.Text = "ID:";
            lblOne.Text = "Customer Name:";
            lblTwo.Text = "Address Line 1:";
            lblThree.Text = "Address Line 2:";
            lblFour.Text = "Town:";
            lblFive.Text = "Postcode:";
            lblSix.Text = "Age:";

            txtID.Text = Convert.ToString(customerList.NewID());
            txtID.Enabled = false;

        }
        else
        {
            this.Text = "Add Product";
            lblZero.Text = "Product ID:";
            lblOne.Text = "Product Code:";
            lblTwo.Text = "Product Name:";
            lblThree.Text = "Product Description:";
            lblFour.Text = "Price:";
            lblFive.Text = " --------- ";
            lblSix.Text = " ---------";

            txtID.Text = Convert.ToString(productList.NewID());
            txtID.Enabled = false;
            txtFive.Enabled = false;
            txtSix.Enabled = false;

        }
    }

表单上的文本框标记为 txtZero.txt ~ txtSix.txt。

非常感谢所有帮助:)

【问题讨论】:

    标签: c# textbox controls


    【解决方案1】:

    我建议只使用NumericUpDown 控件。

    您只能在其中输入数字,并且可以设置允许的最小值和最大值。

    numericUpDown1.Minimum = 1;
    numericUpDown1.Maximum = 99;
    

    【讨论】:

    • 天哪,这太简单了。对不起,当我卸载VS2010并羞愧地低下头时。非常感谢:)
    【解决方案2】:

    你可以添加这个:

        public static void NumericEvent(ref string text, ref KeyPressEventArgs e)
        {
            if (char.IsNumber(e.KeyChar) || e.KeyChar == (char)Keys.Back || e.KeyChar == (char)Keys.Delete)
            {
                e.Handled = false; //ok
            }
            else
            {
                e.Handled = true; //not ok
            }
        }
    

    然后用这个调用:

        private void GenericNumeric_KeyPress(object sender, KeyPressEventArgs e)
        {
            TextBox tb = sender as TextBox;
            string field = tb.Text;
            NumericEvent(ref field, ref e);
            tb.Text = field;
        }
    

    【讨论】:

      【解决方案3】:

      认为这个之前已经讨论过几次了......

      How to allow only integers in a textbox?

      如果您确实希望用户无法输入非数字字符,则需要在 Javascript 中执行此操作。验证器就是这样,它们在表单回发后验证输入。您只需添加验证器并在您的回发方法中添加,例如

      protect void btnSave_Click(object sender, EventArgs e)
      {
          Page.Validate(); // validation message from the validator gets displayed 
      
          if (!Page.IsValid)
              return;  
      }
      

      【讨论】:

      • 啊!再读一遍。我仍然看不到它实际指定 Winforms 或 ASP.NET MVC 或 ASP.NET 的位置。所以......好猜测@GrantWinney
      • 对不起,我已经连续工作了 4 天,昨晚发帖时已经筋疲力尽了。
      猜你喜欢
      • 2011-11-09
      • 1970-01-01
      • 2021-06-21
      • 1970-01-01
      • 2010-10-24
      • 2012-09-18
      • 2013-01-22
      • 1970-01-01
      相关资源
      最近更新 更多