繁体   English   中英

C#Winform文本框验证损坏的取消按钮

[英]C# Winform textbox validating broke cancel button

我正在使用visual studio 2013C#制作winform应用程序。 在我的表格上,我有几个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时,它恰好在我的aTextBox_Validation事件中击中了else 强迫我键入正确的值,然后才能单击“取消”按钮。

我做了一些谷歌搜索,试图弄清楚如何解决它。 我尝试过的一些事情是:

cancelButton.CausesValidation = false; 在我的cancelButton_Click事件中不起作用。

试图设置bool标志来执行与^几乎相同的操作,但这似乎也不起作用。

有任何想法或将其推向正确的位置吗?

您需要设置:

AutoValidate = EnableAllowFocusChange

在包含表格上。

然后,您应该在“提交”按钮上进行验证。 问题是,当您离开文本字段时,始终会触发Validated事件,因此即使您单击“取消”按钮,也会发生此事件。

如果在文本框失去焦点时运行了文本框验证,则单击“取消”按钮将导致文本框失去焦点,从而在关闭之前进行验证。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM