我在表單上有一個關閉按鈕,我已將其設置爲CausesValidation = false
。但是當我運行它並試圖用ErrorProvider關閉它時,它不會讓我關閉它。我甚至嘗試添加到關閉例程errorProvider1.Clear()
或errorProvider1.Dispose() but
仍然沒有運氣關閉窗體,直到我處理錯誤。btnClose設置爲CausesValidation = false但不起作用
我發現工作是關閉,如果我循環所有的控件,並設置CausesValidation=false
然後它按預期的方式通過關閉窗體,儘管出現任何錯誤。你能否提供見解?
private void btnAdd_Click(object sender, EventArgs e)
{
if (!this.ValidateChildren()) return;
DoAddCommand();
}
private void DoAddCommand()
{
//input
string input1 = textBox1.Text;
string input2 = textBox2.Text;
//process
this.ValidateChildren();
decimal sum = Class1.Add(input1, input2);
//output
lblSum.Text = sum.ToString();
}
private void textBoxNumberEntry_Validating(object sender, System.ComponentModel.CancelEventArgs e)
{
TextBox textBox = sender as TextBox;
decimal number1 = 0;
try
{
number1 = decimal.Parse(textBox.Text);
errorProvider1.Clear();
}
catch (FormatException)
{
errorProvider1.SetError(textBox, "Enter a valid number");
e.Cancel = true;
}
}
private void btnClose_Click(object sender, EventArgs e)
{
foreach (Control item in this.Controls)
{
item.CausesValidation = false;
}
this.Close();
}
-1問題是關於Windows窗體,而不是ASP.NET。 – 2013-10-15 17:49:48
即使原來的問題是在Windows窗體方面,我發現這個解決方案對ASP.NET中的類似情況很有幫助。 – 2016-07-27 19:02:30