我的项目中有2个表单。用户无法从frmOptometry中退出第一个。我这样实现了这个:
Private Sub frmOptometry_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
MessageBox.Show("You can only close the application from the receipt screen!")
e.Cancel = True
End Sub
第二种形式,frmReceipt可以退出。我用这种方式实现了它:
Private Sub frmReceipt_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
If MessageBox.Show("Are you sure you want to quit?",
"Obi-Wan Optometry Closing",
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation) =
DialogResult.No Then
e.Cancel = True
Else
frmOptometry.Close()
End If
End Sub
现在,它并没有完全关闭程序,因为它进入frmOptometry_FormClosing并阻止它。我该怎么做才能解决这个问题?无论如何要确定用户试图关闭程序的位置,然后从那里决定?
答案 0 :(得分:0)
解决此问题的一种方法是引入flag
(例如,称为FromReceipt
),表明谁要求frmOptometry
关闭自己。
frmOptometry
上会发生这样的事情:
Public FromReceipt As Boolean = False 'Add this
Private Sub frmOptometry_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If Not FromReceipt Then 'add this condition
MessageBox.Show("You can only close the application from the receipt screen!")
e.Cancel = True
End If
End Sub
然后在frmReceipt
:
Private Sub frmReceipt_FormClosing(sender As Object, e As FormClosingEventArgs) Handles MyBase.FormClosing
If MessageBox.Show("Are you sure you want to quit?",
"Obi-Wan Optometry Closing",
MessageBoxButtons.YesNo,
MessageBoxIcon.Exclamation) =
DialogResult.No Then
e.Cancel = True
Else
frmOptometry.FromReceipt = True 'Add this line to tell the frmOptometry: this time you can really close it
frmOptometry.Close()
End If
End Sub