I have two forms in my WindowsFormApplication
named as Form1 and 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.
I don't want this to happen so then I tried changing Me.Close()
to 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
Me. Close
and 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.
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).
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. 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.
This code goes to 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
:
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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.