[英]Terminate program when Close button is Clicked after Prompt - Visual Basic
I have two forms in my WindowsFormApplication
named as Form1 and Form2. 我的
WindowsFormApplication
有两个窗体,分别称为Form1和Form2。 The idea is when the Program is being closed, it shows up a Dialog Box to confirm it. 这个想法是当程序关闭时,它会显示一个对话框来确认它。
Public Class Form1
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Form2.Show()
Me.Close()
End Sub
Private Sub Form1_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
MessageBox.Show("You are About to cancel the Setup.","Cancel Setup?",
MessageBoxButtons.OK,
MessageBoxIcon.Exclamation,
MessageBoxDefaultButton.Button1)
End Sub
End Class
Until here, my code worked fine but the problem is when I click Button1, the Message Box appears to confirm the closure of Form1. 直到这里,我的代码都可以正常工作,但是问题是当我单击Button1时,出现了消息框,以确认Form1的关闭。
I don't want this to happen so then I tried changing Me.Close()
to Me.Hide
. 我不希望发生这种情况,因此我尝试将
Me.Close()
更改为Me.Hide
。 I was successful for preventing the message box to appear but then I got another Problem. 我成功地阻止了消息框的出现,但是又遇到了另一个问题。 As the Form hides, it stays active in the background and I also don't want this to happen.
表单隐藏时,它在后台保持活动状态,我也不希望这种情况发生。
Another thing I added in Form1_FormClosing
is Me. Close
我在
Form1_FormClosing
添加的另一件事是Me. Close
Me. Close
and Form2.Close
. Me. Close
和Form2.Close
。 This enables to close both the forms once the program's active Form is being closed. 一旦关闭程序的活动窗体,就可以关闭两个窗体。 But Again, there's a problem.
但是,这又是一个问题。 As soon as I click the close button, the Message Boxes fill up the screen and not listening to my Command.
单击关闭按钮后,“消息框”将填满整个屏幕,而不会收听我的命令。 Anyone got a solution for this?
有人对此有解决方案吗?
So.. 所以..
If the user clicks close in Form1
, then the program should terminate with no messagebox. 如果用户在
Form1
单击“关闭”,则程序应终止而不显示任何消息框。
If the user clocks close in Form2
, Form3
or Form4
, the program would Terminate when the user clicks Yes
in the MessageBox
or else nothing would get affected (Especially the Data). 如果用户在
Form2
, Form3
或Form4
关闭时钟,则当用户在MessageBox
单击“ Yes
时,程序将终止,否则将不受影响(尤其是数据)。
The way this works is when user clicks Close in Form1
, the program terminates with no MessageBox
and if the user clicks Close in other Forms
, A MessageBox
would appear asking whether you are sure to close the Program. 这种工作方式是当用户在
Form1
单击“关闭”时,程序终止而没有MessageBox
并且如果用户单击“其他Forms
”中的Forms
关闭Forms
,则会出现一个MessageBox
,询问您是否确定要关闭程序。 If the user clicks Yes, the program will Terminate because the DialogResult
is Yes and Form1.Closing
Cancel goes to False and Closes Form1
(As Closure type was set as First Form Closes in Program Properties, this will terminate the program) Else the Form1.Closing
Cancel goes to True which will prevent the current Form from closing and losing any Data. 如果用户单击“是”,则该程序将终止,因为
DialogResult
为“是”并且Form1.Closing
Cancel变为False并关闭Form1
(由于Closure类型在“程序属性”中设置为“ First Form Closes”,因此将终止该程序) Form1.Closing
取消变为True,这将防止当前表单关闭并丢失任何数据。
This code goes to Form1
: 此代码转到
Form1
:
Imports System.ComponentModel
Public Class Form1
Friend closeProgramAlreadyRequested As Boolean = False
Private Sub Form1_Closing(sender As Object, e As CancelEventArgs) Handles Me.Closing
closeProgramAlreadyRequested = False
End Sub
End Class
This code goes to Form2
& applies to other Forms
as well but except for Form1
: 此代码适用于
Form2
,并且也适用于其他Forms
,但Form1
除外:
Imports System.ComponentModel
Public Class Form2
Private Sub Form2_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
If Form1.closeProgramAlreadyRequested = False Then
Dim result As DialogResult = MessageBox.Show("You are About to cancel the Setup.", "Cancel Setup?", MessageBoxButtons.YesNo, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1)
If result = DialogResult.No Then
e.Cancel = True
Else
e.Cancel = False
Form1.Close()
End If
End If
End Sub
End Class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.