验证多个文本框的文本?

我有一个 WinForm(c#),组框中有 10 个文本框


应该只允许用户输入数字 1 到 100: -- 所有文本框都在一个 GroupBox 中 -- 如果有帮助的话


如何将下面提到的代码应用于多个文本框?


    TextBox tb = sender as TextBox;

    if (tb != null)

    {

     int i;

     if (int.TryParse(tb.Text, out i))

     {

      if (i >= 0 && i <= 100)

      return;

     }

    }

    MessageBox.Show("Please enter a number from 1 - 100");

http://img3.mukewang.com/64bb8da40001e1a403760169.jpg

单击“添加”后,如果用户输入的数字超过 100 或什么都没有,则会弹出一个警告窗口

阿晨1998
浏览 173回答 5
5回答

慕慕森

您可以为离开事件定义一个方法,并将该方法设置为属性窗口中每个文本框的离开事件:private void TextBoxGroup_Leave(object sender, TextBox e){&nbsp; &nbsp; if (Convert.ToInt32(e.Text) < 1 || Convert.ToInt32(e.Text) > 100)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("Please enter a number from 1 - 100");&nbsp; &nbsp; &nbsp; &nbsp; e.Text = string.Empty;&nbsp; &nbsp; }}

青春有我

下面是使用文本框的离开事件的示例。此外,文本框按键事件用于仅允许数字。将每个文本框Leave和KeyPress事件连接到下面的文本框和事件,它应该按照您所描述的方式工作。正如其他人提到的,有很多方法可以做到这一点。private void tb_Course_Leave(object sender, EventArgs e) {&nbsp; TextBox tb = sender as TextBox;&nbsp; if (tb.Text == "")&nbsp; &nbsp; return;&nbsp; if (int.TryParse(tb.Text, out int value)) {&nbsp; &nbsp; if (value >= 0 && value <= 100) {&nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; }&nbsp; }&nbsp; MessageBox.Show("Please enter a VALID number from 1 - 100");&nbsp; tb.Text = "";}private void tb_Course_KeyPress(object sender, KeyPressEventArgs e) {&nbsp; e.Handled = !char.IsDigit(e.KeyChar);}&nbsp;

慕森卡

您可以循环遍历 GroupBox 中的每个控件,并检查哪些是文本框:ForEach (Control tb in gb.Controls) //gb is a reference your GroupBox object{&nbsp; if tb is TextBox&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; int i;&nbsp; &nbsp; &nbsp; &nbsp; if (int.TryParse(tb.Text, out i))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (i >= 0 && i <= 100)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; MessageBox.Show("Please enter a number from 1 - 100");}在缺乏关于您正在做什么的更多背景的情况下,很难优化您的验证过程,但这回答了您的问题。

冉冉说

如果您想验证多个组框中文本框的输入,您可以尝试此操作。&nbsp;private void ValidateInputOfControls(Control.ControlCollection[] controlsArray)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; foreach (Control.ControlCollection controlCollection in controlsArray)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; foreach (Control control in controlCollection)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (control is TextBox)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int result;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (int.TryParse(control.Text, out result))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!(result >= 0 && result <= 100))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("Please enter a number from 1 - 100 in field " + control.Name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; else&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; MessageBox.Show("Please enter a number from 1 - 100 in field " + control.Name);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }您需要做的就是将组框的控件添加到 Control.ControlCollection 数组中,然后调用该方法。请参阅下面的示例。Control.ControlCollection[] controls = { groupBox1.Controls, groupBox2.Controls };ValidateInputOfControls(controls);

HUWWW

对于复杂的形式MessageBox.Show可能会变得相当烦人:它不会将用户指向错误的字段它一一显示错误,您可能还有更多不正确的输入还建议的方法“允许”用户通过单击“添加”按钮来失败。我建议使用ErrorProvider将用户指向失败的输入,并在模型(在您的情况下 - 所有输入)无效时禁用按钮。您甚至可以限制用户在提供错误输入时不要留下字段:)您可以阅读Windows 窗体中的用户输入验证指南,它概述了 WindowsForms 中的验证工具。public partial class Form1 : Form    {        private HashSet<TextBox> validatedInputs = new HashSet<TextBox>();        private int inputsCount;        public Form1()        {            InitializeComponent();            button1.Enabled = false;            foreach (var tb in groupBox1.Controls.OfType<TextBox>())            {                tb.Validating += allTextBoxs_ValidatingAndEditing;                tb.TextChanged += allTextBoxs_ValidatingAndEditing;                inputsCount++;            }        }        private void allTextBoxs_ValidatingAndEditing(object sender, EventArgs e)        {            if (sender is TextBox tb)            {                if (!int.TryParse(tb.Text, out int value) || value < 0 || value > 100)                {                    if (e is CancelEventArgs ce)                        ce.Cancel = true;                    errorProvider1.SetError(tb, "Value must be in range [0..100]");                    validatedInputs.Remove(tb);                }                else                {                    errorProvider1.SetError(tb, null);                    validatedInputs.Add(tb);                }                setAddButtonEnabled();            }        }        private void setAddButtonEnabled() => button1.Enabled = validatedInputs.Count == inputsCount;    }请注意,此实现的限制非常严格,用户甚至无法关闭表单,直到输入正确的值:)
打开App,查看更多内容
随时随地看视频慕课网APP