问题描述
我正在开发WinForms应用程序,并遇到了一个非常奇怪的错误。将MessageBox放入FormClosing事件时出现问题,该事件导致主窗体调整大小并取消了自定义对话框上的显示。除了MessageBox之外,我还取出了其他所有代码来确认问题。 Windows PC桌面上不存在此问题,而仅在MS Surface Go上存在。这是FormClosing的工作代码
Private Sub Form1_FormClosing(ByVal sender As Object,ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
Dim form As New frm_NumEnter
form.isPassword = True
form.String1 = closeString
If form.ShowDialog(Me) = DialogResult.OK Then
If form.result = "####" Then
form.Close()
Else
'MessageBox.Show("Password is Wrong. Please Retry.") Do not turn this on. Causes display issues.
form.Close()
Form1_FormClosing(sender,e)
End If
Else
e.Cancel = True
End If
End If
End Sub
消息框当前已被注释掉,因为这是工作代码。
任何人都不知道为什么会这样吗?我一直在寻找答案,却找不到答案。
解决方法
基于汉斯的评论。
问题在于,Surface Go默认情况下正在调用WPF MessageBox类。使用对Forms.MessageBox.Show(...)
的MessageBox类的更明确的调用解决了此问题。这仅在与应用程序一起关闭的表单上发生。我不确定为什么会这样,但我推测是因为WPF不会像WinForms那样使用FormClosing事件。
尝试一下
Private Sub Form1_FormClosing(sender As Object,e As FormClosingEventArgs) Handles Me.FormClosing
If e.CloseReason = CloseReason.UserClosing Then
e.Cancel = True
Dim form As New frm_NumEnter
form.isPassword = True
form.String1 = "closeString"
If form.ShowDialog(Me) = DialogResult.OK Then
If form.result = "####" Then
form.Close()
e.Cancel = False
Else
MessageBox.Show("Password is Wrong. Please Retry.")
form.Close()
End If
End If
End If
End Sub