我正在制作一个winform
应用程序C#
与visual studio 2013
。在我的表格上,我有几个text-boxes
和几个buttons
。C#Winform文本框验证中断取消按钮
更具体地说,我有一个cancelButton
,当点击将关闭应用程序,这是与这个事件在这里处理。
private void cancelButton_Click(object sender, EventArgs e)
{
//close the application
Application.Exit();
}
我有一个TextBox_Validated
事件看起来是这样的
private void aTextBox_Validated(object sender, EventArgs e)
{
int matchIntPosition;
string someString = aTextBox.Text.ToUpper();
//check a string array to see if the value entered exsists
matchIntPosition= Array.IndexOf(someStringArray, someString);
if (matchIntPosition > -1)
{
string someString = someStringArray[matchIntPosition];
}
else
{
MessageBox.Show("This value does not exist, Please try again.");
aTextBox.Focus();
}
}
现在我强迫用户,以确保继续我的形式之前,这个领域是正确填写(这是第一次我的表单上的文本框)。
这是我的问题所在,当我点击我的cancelButton时,它只是在我的aTextBox_Validation
事件中点击了else
。强制我输入正确的值,然后才能点击取消按钮。
我已经做了一些谷歌试图找出我如何解决它。我试过的一些东西是:
cancelButton.CausesValidation = false;
在我的cancelButton_Click
事件中,没有工作。
试图设置一个bool
标志做与^几乎相同的事情,但似乎也没有工作。
任何想法或推到正确的地方?
哪里声明someStringArray变量?它是否包含someString变量值? – yosbel
我会尝试验证提交按钮单击上的所有文本框。这是很正常的形式行为。它可以解决您的问题,并防止最终客户对您继续关注它们感到沮丧。 – Jonesopolis
@yosoy阵列在课堂上,你错过了我的问题。 – Zoxac