【问题标题】:How to prevent a form from closing without using the FormClosing event如何在不使用 FormClosing 事件的情况下防止表单关闭
【发布时间】:2014-04-12 03:19:07
【问题描述】:

我最近开始使用 Visual Studio 2010 研究 VB.net,到目前为止我做得很好,但是我已经被这个问题困扰了几个小时,并且在谷歌和堆栈溢出搜索的许多页面之后,我来找你。

我有 2 种表格,一种是您将人员数据放入数据库,另一种是允许您根据文档类型和文档编号进行搜索,我们将它们称为 Form1 和 Form2。问题是,当用户在表单 2 上按 OK 并且文档或文档类型为空时,一个 msgbox 会显示告诉他们填写文本框,但之后表单会关闭,所以我想要做的是防止表单关闭,如果用户按确定,任何字段为空。

我在 form2 上的 OK 按钮单击事件(我想阻止表单关闭):

Private Sub cmd_ok_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_ok.Click

    If Me.txt_dni.Text = "" Then
        MsgBox("Numero Documento vacio", vbOKOnly + vbCritical, "Atencion")
        Me.txt_dni.Focus()
        Exit Sub
    End If

    If Me.cmb_tdoc.SelectedIndex = -1 Then
        MsgBox("Tipo de Documento vacio", vbOKOnly + vbCritical, "Atencion")
        Me.cmb_tdoc.Focus()
        Exit Sub
    End If

End Sub

我从 Form2 获取值到它的父级 form1;当我需要同时使用文档类型和文档编号在 datagridview 上查看某人时,我会调用此函数:

私有函数 abrir_form2(ByRef rdoc2 As Integer, ByRef rtdoc2 As Integer)
        暗淡对话框 As Form2
        将 doc2 调暗为整数
        将 tdoc2 调暗为整数

    dialog = New Form2()
    Dim result As DialogResult = dialog.ShowDialog(Me)

    doc2 = dialog.doc2
    tdoc2 = dialog.tdoc2
    rdoc2 = doc2
    rtdoc2 = tdoc2

    Return vbNull
End Function

两种形式:http://i.imgur.com/n1EwfiW.png

也许还有另一种方法?有没有其他办法解决这个问题?

提前致谢!

【问题讨论】:

  • 如果您没有真正返回任何东西,只需将其设为 Sub 而不是 Function

标签: vb.net forms formclosing


【解决方案1】:

我的猜测是您将ButtonDialogResult 属性设置为None 以外的其他值。在这种情况下,单击 Button 会将相同的值分配给表单的 DialogResult 属性。如果表单使用ShowDialog 显示,设置其DialogResult 属性将关闭它。如果您不想每次在单击Button 时关闭表单,则不要设置其DialogResult 属性。相反,当且仅当您想要关闭表单时,才在ButtonClick 事件中显式设置表单的DialogResult 属性。

【讨论】:

    猜你喜欢
    • 2012-07-13
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 1970-01-01
    • 2021-05-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多