我想提出一个winform
应用程序C#
与visual studio 2013
。在我的表格上,我有text-boxes
几个buttons
。
更具体地说,我有一个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时,它恰好触及了else
我的aTextBox_Validation
事件。强迫我键入正确的值,然后才能单击“取消”按钮。
我做了一些谷歌搜索,试图弄清楚如何解决它。我尝试过的一些事情是:
cancelButton.CausesValidation = false;
在我的cancelButton_Click
活动范围内,没有任何效果。
试图设置一个bool
标志来执行与^几乎相同的操作,但这似乎也不起作用。
有任何想法或将推向正确位置的方法吗?
然后,您应该在“提交”按钮上进行验证。问题是,Validated
当您离开文本字段时,总是会触发该事件,因此即使您单击“取消”按钮,也会发生这种情况。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句