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;
}
}

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);
}

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");
}
在缺乏關于您正在做什么的更多背景的情況下,很難優化您的驗證過程,但這回答了您的問題。

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);

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;
? ? }
請注意,此實現的限制非常嚴格,用戶甚至無法關閉表單,直到輸入正確的值:)
- 5 回答
- 0 關注
- 215 瀏覽
添加回答
舉報