是否有任何方式打开该死错误提供商断开当我尝试使用windows关闭按钮(X),以关闭该窗体。它激发了验证和用户必须填写所有字段,才可以关闭form..this将是一个可用性问题,因为很多人都倾向于使用(X)按钮来关闭表单。

我已经放置与导致验证为false取消按钮,它也触发一个验证。

我发现有人说,如果你使用Form.Close()函数验证的运行... 我怎样才能让过去这个恼人的功能。

我有一个MDI sturucture和示出了使用的形式

 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

任何帮助十分赞赏。 谷歌搜索仅表现在那里使用者使用命令按钮,关闭按钮具有问题的结果和在我的情况下,过多引起问题

有帮助吗?

解决方案

在ValidateChildren()方法防止形式从闭合。在表单这个代码粘贴到修复:

protected override void OnFormClosing(FormClosingEventArgs e) {
  e.Cancel = false;
}

其他提示

这是很简单的解决,在你的窗体的Closing事件,设置一个标志,表明离开的形式,例如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

编辑:修订这个答案列入按OP的评论。我的建议是处理表单的内部活动,如图

    Private Sub Form1_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
        blnLeave = True
    End Sub

和这里处理它在窗体的窗口过程重写如下所示....

    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
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top