Vb.net Errorprovider显示使用windows关闭按钮时出错(X)
当我尝试使用windows关闭按钮(X)关闭窗体时,是否有任何方法关闭该死的错误提供程序。它触发验证,用户必须填写所有字段才能关闭表单。这将是一个可用性问题,因为许多人倾向于使用(X)按钮关闭表单 我已经放置了一个取消按钮,它会导致验证为false,并且还会触发验证 我发现有人说,如果使用Form.Close()函数,将运行验证。。。 我怎样才能克服这个恼人的特性 我有一个MDI结构,并使用Vb.net Errorprovider显示使用windows关闭按钮时出错(X),vb.net,winforms,validation,errorprovider,Vb.net,Winforms,Validation,Errorprovider,当我尝试使用windows关闭按钮(X)关闭窗体时,是否有任何方法关闭该死的错误提供程序。它触发验证,用户必须填写所有字段才能关闭表单。这将是一个可用性问题,因为许多人倾向于使用(X)按钮关闭表单 我已经放置了一个取消按钮,它会导致验证为false,并且还会触发验证 我发现有人说,如果使用Form.Close()函数,将运行验证。。。 我怎样才能克服这个恼人的特性 我有一个MDI结构,并使用 CreateExam.MdiParent = Me CreateExam.Show()
CreateExam.MdiParent = Me
CreateExam.Show()
在mdi父菜单项上,单击
并将此设置为验证
Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If String.IsNullOrEmpty(TextBox1.Text) Then
Err.SetError(TextBox1, "required")
e.Cancel = True
End If
If TextBox1.Text.Contains("'") Then
Err.SetError(TextBox1, "Invalid Char")
e.Cancel = True
End If
End Sub
非常感谢您的帮助。
谷歌搜索只显示用户在使用命令按钮作为关闭按钮时遇到问题的结果,而这也导致了问题。在我的例子中,这是一个非常简单的修复方法,在表单的
关闭事件中,设置一个标志以指示在加载表单时离开表单,例如blnLeave
,将标志设置为False
,当触发Closing
事件时,在该事件处理程序中将该标志设置为True
,然后将如下更改
Private Sub TextBox1_Validating(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles TextBox1.Validating
If (blnLeave) Then
e.Cancel = False;
Return
End If
If String.IsNullOrEmpty(TextBox1.Text) Then
Err.SetError(TextBox1, "required")
e.Cancel = True
End If
If TextBox1.Text.Contains("'") Then
Err.SetError(TextBox1, "Invalid Char")
e.Cancel = True
End If
End Sub
私有子TextBox1_验证(ByVal sender作为System.Object,ByVal e作为System.ComponentModel.CancelEventArgs)处理TextBox1.Validating
如果(blnLeave)那么
e、 取消=假;
返回
如果结束
如果String.IsNullOrEmpty(TextBox1.Text),则
错误设置错误(文本框1,“必需”)
e、 取消=真
如果结束
如果TextBox1.Text.包含(“”),则
Err.SetError(TextBox1,“无效字符”)
e、 取消=真
如果结束
端接头
编辑:根据OP的意见修改了此答案,以便纳入。我的建议是处理表单的关闭事件,如图所示
Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
blnLeave = True
End Sub
私有子表单1_FormClosed(ByVal发送方作为System.Object,ByVal e作为System.Windows.Forms.FormClosedEventArgs)处理MyBase.FormClosed
blnLeave=True
端接头
并在窗体的窗口过程覆盖中处理它,如下所示
Private Const SC_CLOSE As Integer = &HF060
Private Const WM_MENUSELECT As Integer = &H11F
Private Function LoWord(ByVal Num As Integer) As Integer
LoWord = Num & &HFFFF
End Function
Protected Overrides Sub WndProc(ByRef m As System.Windows.Forms.Message)
If m.Msg = WM_MENUSELECT Then
If LoWord(m.WParam.ToInt32()) = SC_CLOSE Then
' Handle the closing via system Menu
blnLeave = True
End If
End If
MyBase.WndProc(m)
End Sub
Private Const SC_CLOSE As Integer=&HF060
私有常量WM_菜单选择为整数=&H11F
私有函数LoWord(ByVal Num作为整数)作为整数
LoWord=Num&&HFFFF
端函数
受保护的覆盖子WndProc(ByRef m As System.Windows.Forms.Message)
如果m.Msg=WM\u菜单选择,则
如果LoWord(m.WParam.ToInt32())=SC\U CLOSE,则
'通过系统菜单处理关闭
blnLeave=True
如果结束
如果结束
MyBase.WndProc(m)
端接头
ValidateChildren()方法防止表单关闭。将此代码粘贴到表单中以修复以下问题:
protected override void OnFormClosing(FormClosingEventArgs e) {
e.Cancel = false;
}
非常感谢…它可以工作…但在第二次单击时…我一单击关闭按钮,它就会显示错误,第二次单击它就可以关闭表单…有什么方法可以阻止它吗?@Pankaj:处理表单。关闭事件并在其中设置标志…您可能需要通过子类化表单来添加覆盖…我将修改此答案为了说明这一点……感谢您花时间回答。我将使用nobugz答案作为公认的解决方案。