亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

驗證多個文本框的文本?

驗證多個文本框的文本?

C#
阿晨1998 2023-07-22 16:04:34
我有一個 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");單擊“添加”后,如果用戶輸入的數字超過 100 或什么都沒有,則會彈出一個警告窗口
查看完整描述

5 回答

?
慕慕森

TA貢獻1856條經驗 獲得超17個贊

您可以為離開事件定義一個方法,并將該方法設置為屬性窗口中每個文本框的離開事件:


private void TextBoxGroup_Leave(object sender, TextBox e)

{

    if (Convert.ToInt32(e.Text) < 1 || Convert.ToInt32(e.Text) > 100)

    {

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

        e.Text = string.Empty;

    }

}


查看完整回答
反對 回復 2023-07-22
?
青春有我

TA貢獻1784條經驗 獲得超8個贊

下面是使用文本框的離開事件的示例。此外,文本框按鍵事件用于僅允許數字。將每個文本框Leave和KeyPress事件連接到下面的文本框和事件,它應該按照您所描述的方式工作。正如其他人提到的,有很多方法可以做到這一點。


private void tb_Course_Leave(object sender, EventArgs e) {

  TextBox tb = sender as TextBox;

  if (tb.Text == "")

    return;

  if (int.TryParse(tb.Text, out int value)) {

    if (value >= 0 && value <= 100) {

      return;

    }

  }

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

  tb.Text = "";

}



private void tb_Course_KeyPress(object sender, KeyPressEventArgs e) {

  e.Handled = !char.IsDigit(e.KeyChar);


查看完整回答
反對 回復 2023-07-22
?
慕森卡

TA貢獻1806條經驗 獲得超8個贊

您可以循環遍歷 GroupBox 中的每個控件,并檢查哪些是文本框:


ForEach (Control tb in gb.Controls) //gb is a reference your GroupBox object

{

  if tb is TextBox

    {

        int i;

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

        {

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

            return;

        }

    }

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

}

在缺乏關于您正在做什么的更多背景的情況下,很難優化您的驗證過程,但這回答了您的問題。


查看完整回答
反對 回復 2023-07-22
?
冉冉說

TA貢獻1877條經驗 獲得超1個贊

如果您想驗證多個組框中文本框的輸入,您可以嘗試此操作。


 private void ValidateInputOfControls(Control.ControlCollection[] controlsArray)

    {          

        foreach (Control.ControlCollection controlCollection in controlsArray)

        {

            foreach (Control control in controlCollection)

            {

                if (control is TextBox)

                {

                    int result;

                    if (int.TryParse(control.Text, out result))

                    {

                        if (!(result >= 0 && result <= 100))

                        {

                            MessageBox.Show("Please enter a number from 1 - 100 in field " + control.Name);


                        }

                    }

                    else

                    {

                        MessageBox.Show("Please enter a number from 1 - 100 in field " + control.Name);

                    }

                 }

            }               

        }

    }

您需要做的就是將組框的控件添加到 Control.ControlCollection 數組中,然后調用該方法。請參閱下面的示例。


Control.ControlCollection[] controls = { groupBox1.Controls, groupBox2.Controls };

ValidateInputOfControls(controls);


查看完整回答
反對 回復 2023-07-22
?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

對于復雜的形式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;

? ? }

http://img2.sycdn.imooc.com/64bb8ddf0001b67103340169.jpg

請注意,此實現的限制非常嚴格,用戶甚至無法關閉表單,直到輸入正確的值:)



查看完整回答
反對 回復 2023-07-22
  • 5 回答
  • 0 關注
  • 215 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號