Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 关闭按钮在windows窗体应用程序中不起作用_.net_C# 4.0 - Fatal编程技术网

.net 关闭按钮在windows窗体应用程序中不起作用

.net 关闭按钮在windows窗体应用程序中不起作用,.net,c#-4.0,.net,C# 4.0,我正在通过验证和已验证事件验证我的文本框,下面是我的代码 private void tbms_Validating(object sender, CancelEventArgs e) { if (tbms.Text.Length==0) { MessageBox.Show("Ms is Empty"); e.Cancel = true; } } private voi

我正在通过验证和已验证事件验证我的文本框,下面是我的代码

    private void tbms_Validating(object sender, CancelEventArgs e)
    {
        if (tbms.Text.Length==0)
        {
            MessageBox.Show("Ms is Empty");
            e.Cancel = true;
        }
    }

    private void tbms_Validated(object sender, EventArgs e)
    {
        MessageBox.Show("No Error");
    }
它的工作原理很好,但我面临的问题是,如果文本框中没有文本,我想通过控制框上的“取消”按钮关闭应用程序,它会显示消息框Ms为空,并再次向窗口提示我。当我在文本框中输入一些文本并单击“取消”按钮时,应用程序关闭。请给出如何解决此问题的提示。提前感谢。
在关闭表单时,也会进行验证。如果验证事件将e.Cancel属性设置为true,则默认FormClosing事件将停止表单关闭。您可以这样解决这个问题:

private void CancelButton_Click(object sender, EventArgs e) {
    this.AutoValidate = System.Windows.Forms.AutoValidate.Disable;
    this.Close();    // or this.DialogResult = DialogResult.Cancel
}
您需要在取消按钮上将“CausesValidation”属性设置为false

  • 在表单中将属性
    KeyPreview
    设置为
    True
  • 添加一个按钮并写入ESC代码(例如
    Me.Dispose()
  • 在表单中,从“取消按钮属性列表”中选择按钮
  • 按钮必须始终为
    Visible=True
    才能工作。如果不想显示它,请将按钮放在任何其他对象后面,或将其定位在
    顶部=-100
    左侧=-1
    处。但是按钮必须始终是可见的

  • 重复:@RJ Lohan,我可以使用一个额外的按钮,将其命名为cancel button并将原因验证设置为false,或将auto validate设置为disable等,但我不想使用额外的按钮关闭应用程序,我只想使用控制框上的cancel按钮。谢谢,您的意思是我必须使用一个额外的按钮来关闭应用程序,并且必须隐藏控制框。您还可以实现FormClosing事件处理程序并强制e.Cancel属性返回false。非常感谢,先生,这就是我正在搜索的内容。这就是问题所在:按钮始终必须可见=true