0

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 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?

Shahroz Asif
  • 19
  • 1
  • 8
  • First off, why is that messagebox there? You don't ask if the user wants to really cancel the setup, you just notify them that they did and all they can do is click OK so it's not really a relevant dialog to the situation. Explain what you are trying to achieve here and someone can provide a better solution. – Charles May Feb 23 '16 at 14:33
  • When you do this, it now becomes Form2's job to display the "Are you sure" message. So you need a variable of type Boolean to suppress the message box, set it to True in your Click event handler. Do consider ShowDialog() as the commonly used alternative so your Form1 stays the "main window". Or the simple alternative for a 'wizard' style user interface, a TabControl [without visible tabs](http://stackoverflow.com/a/2798241/17034). Or simply two UserControls and one form, use their Visible property. – Hans Passant Feb 23 '16 at 14:45
  • This code applies to all forms (Form1 - Form4) and can you give the improved answer support in Code if Possible? – Shahroz Asif Feb 23 '16 at 15:12
  • And yeah, a lot of Mistakes can happen. If the user clicks the Close button by mistake and they made a huge progress through the program, he would get exhausted. That's why I want to Include the lifesaver Message box in my Program. – Shahroz Asif Feb 23 '16 at 15:14
  • This is why I was saying that you should ask if the user want to cancel the setup and give them an option to press yes or no and react to their answer.Right now you just tell them they've cancelled and all they can do is click OK. But I agree with @Hans, you should do this on a single form and switch to different panels or tabs as the user progresses. – Charles May Feb 23 '16 at 15:16
  • The 'YesNo' MessageBox doesn't function properly. If the user clicks either Yes or No, The Program Closes. – Shahroz Asif Feb 23 '16 at 15:35

1 Answers1

0

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
Shahroz Asif
  • 19
  • 1
  • 8
David Wilson
  • 4,369
  • 3
  • 18
  • 31
  • This is for a single form. I want to Terminate the whole program once the user clicks `Yes` in the `MessageBox`. – Shahroz Asif Feb 27 '16 at 16:08
  • OK so.. If someone clicks close on any Form, you want the warning message to pop up, and if they click Yes, then close the whole app. – David Wilson Feb 27 '16 at 21:12
  • Oh by the way, in your original post, you said that you didnt want form1 to be hidden, but closed? Unfortunately, If you close Form1, the whole program will close. For the other forms to stay open, Form1 must only be hidden. – David Wilson Feb 27 '16 at 21:16
  • I was just trying to explain my code in my Question. I want the whole program to Terminate once the user closes the open form. It doesn't matter whether `form1` gets hidden or Closes because I can change the Closure type to either "First form closes" or "Last Active form Closes". – Shahroz Asif Feb 28 '16 at 13:37
  • OK. Let me see if I have this. If only `Form1` has been opened, and the user click close, then the program should terminate with no prompts. If you have other forms open, and the user clicks to close `Form1` or any of the other forms, you want one message prompt to appear and if the user selects yes, then the whole program is terminated. If the user clicks no, then no forms are closed. Does that sound right? – David Wilson Feb 28 '16 at 23:46
  • Okay fine, this Idea is good. I think I know for this type of code. First I have to set Close Type to "When the First form Closes". Then I don't need to write anything in Form1's `FormClosing`, I just have to write it in the rest existing form's `FormClosing`. The `FormClosing` will contain the code which you helped to update and if I want, I can change some parts of the code.The way this works is `Form1` is said to be the main Closure form. If it closes, the program Terminates. Now when the user closes `Form1`, It closes with no Prompt and when user closes some other form, It closes `Form1`. – Shahroz Asif Feb 29 '16 at 11:50
  • But there is a problem here as well... When the user clicks "No" in the Message Box, the active Form Closes but not the program. I want the "No" to bring back the last Active `Form` or I want the "No" to Cancel the `FormClosing` Process. – Shahroz Asif Feb 29 '16 at 11:53
  • @ShahrozAsif OK. I've updated my anwer - I hope it is better suited to you – David Wilson Mar 01 '16 at 00:00
  • Okay, so if the user clicks no in `Form2`, will the old data get deleted? Eg. The user writes something in `TextBox` and then clicks `No` in the `MessageBox`. Will the text stay the same? – Shahroz Asif Mar 01 '16 at 18:00
  • I'm at work at the moment and can't check, but all the data should be intact if the form isn't closed. – David Wilson Mar 01 '16 at 18:06
  • Hi, I've updated the answer - this seems to work ok. – David Wilson Mar 02 '16 at 23:47
  • I didn't mean to Open Multiple `Form2's` at Once nor Show the `MessageBox` inside `WarnIfFormIsOpen`. The Code you gave before was similar to as I wanted. If possible, can you please Re-write or recover the Older Code? Or I'll try to recover it with the existing Code. – Shahroz Asif Mar 03 '16 at 09:46
  • I wrote my code as Edit to your answer. Please accept the Edits so that I can mark this answer as a Solution to my Problem. – Shahroz Asif Mar 03 '16 at 18:59
  • I have another question left to be answered. Is it possible that you can answer it? The link: http://stackoverflow.com/questions/35898196/add-objects-code-in-another-class-library-project-with-reference-visual-basic – Shahroz Asif Mar 10 '16 at 18:15