Vb.net 如何中止模式框关闭?

Vb.net 如何中止模式框关闭?,vb.net,modal-dialog,Vb.net,Modal Dialog,我试图在允许模式框关闭之前验证一些数据,但它似乎在验证之前关闭。这是我的密码 Public Class my_popup Inherits Form Dim result = Me.ShowDialog() If result = DialogResult.OK Then If key = Nothing Then If last_name.Text <> "" Then MessageBox.Show("The user is not in

我试图在允许模式框关闭之前验证一些数据,但它似乎在验证之前关闭。这是我的密码

Public Class my_popup

Inherits Form

Dim result = Me.ShowDialog()
If result = DialogResult.OK Then
   If key = Nothing Then
      If last_name.Text <> "" Then
         MessageBox.Show("The user is not in the database.")
        ' Abort closing and leave dialog open.
      End If
   Else
     save_it()
   End If
End If

End Class
公共类我的\u弹出窗口
继承形式
Dim result=Me.ShowDialog()
如果结果=DialogResult.OK,则
如果key=Nothing,则
如果last_name.Text“”则
Show(“用户不在数据库中。”)
'中止关闭并保持对话框打开。
如果结束
其他的
保存它
如果结束
如果结束
末级

当消息框显示时,模态窗体已关闭。如何防止这种情况发生?

您的代码似乎有点奇怪。通常,从需要一些输入的父窗体调用模态对话框,并等待模态对话框完成其工作。如果您需要在模态对话框中进行某种验证,并且在验证失败时保持模态对话框打开,那么您应该编写如下内容

以电话形式:

Using frm = new my_popup()
   if frm.ShowDialog() = DialogResult.OK Then
      ..... if we enter here the validation is OK...
   End If
End Using
然后,在
my_弹出窗口
类中,通常有一个OK按钮(本例中称为buttonOK),该按钮被设置为窗体的AcceptButton,其属性DialogResult被设置为DialogResult.OK

然后,当您单击OK按钮时,您将在按钮的事件处理程序中写入验证

Protected Sub buttonOK_Click(sender as Object, e as EventArgs) Handles buttonOK.Click

      If last_name.Text <> "" Then
          MessageBox.Show("The user is not in the database.")
          ' Abort closing and leave dialog open.
          Me.DialogResult = DialogResult.None
      Else
          Dim saved = save_it()
          if saved = false Then
              MessageBox.Show(....fail to save....)
              Me.DialogResult = DialogResult.None
          End If
      End If
End Sub
Protected Sub buttonOK\u Click(发送者作为对象,e作为事件参数)处理buttonOK。单击
如果last_name.Text“”则
Show(“用户不在数据库中。”)
'中止关闭并保持对话框打开。
Me.DialogResult=DialogResult.None
其他的
Dim saved=保存它()
如果保存=false,则
MessageBox.Show(..保存失败…)
Me.DialogResult=DialogResult.None
如果结束
如果结束
端接头

这里的关键是将my_弹出窗体的属性DialogResult设置为DialogResult.None。这将阻止表单的自动关闭,您的用户可以对输入键入必要的修复…

Me.ShowDialog()?你在哪个类的哪个方法中调用这个?我编辑它来显示这个类。你只是把MessageBox.show()调用放错了位置。它只能在对话框关闭后运行。将它放在它所属的位置,在“确定”按钮中单击事件处理程序。当您不满意时,不要设置DialogResult属性。您可以循环该属性,直到结果正常为止。如果没有,请重新打开itOK,谢谢。我会的。