Query Unload?

zimzon

Member
Joined
Oct 31, 2006
Messages
13
Programming Experience
Beginner
Hi to all,

im very newbie to vb.net.

i want to execute the some code such "Do you want to exit now?" in msgbox. If I click "Yes" the form will close otherwise form will resume current status.

Pls help me.
 
Something like this should do the job...

VB.NET:
If MessageBox.Show("Do you want to exit now?", "Exit application",     MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.Yes Then
            Application.Exit()
End If

Edit: If you want to close the form but not the whole application then Call Me.Close() rather than Application.Exit.

Hope this helps,

Regards,

Andy
 
you can set it up where ever you want, either set up a button called exit, or a menu item called exit, so it's fired when when the user clicks it, but in doing that, having a msgbox appear might be a little silly
 
As redmo says, you can place the code where ever you like.. try this.

Place a new button on the form. Go to its properties and set the Text property to say "Exit" or "Close". Go back to the form and double click on the button. The code window will open and has made a new sub routine for you, what ever you put in there will run when the button is clicked. So paste the code above in and see what happens when you run the application.

Edit:

If you want this message to appear when the user attempts to close the form in any way then you need to handle the forms Closing event. You can do this by switching to the code view (right click: view code). You'll see two drop downs at the top. In the left hand drop down select (Form1 Events) (this might not be Form1). In the right hand drop down select closing. This will make the event handler for you. Then paste the following code in:-

VB.NET:
If MessageBox.Show("Do you want to exit now?", "Exit application",     MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
      e.Cancel
End If

Regards,

Andy
 
I tried this code in Form Closing event. When I try to close the form using Forms' Close button, the msgbox appeares two times and then form closed.

I want, the msgbox will appeare once.
 
I'm not sure why it's appearing twice? However I did make a slight mistake, it should be e.Cancel = true rather than e.Cancel. If your still having problems post the event handler code. It should look like this:-

VB.NET:
Private Sub Form1_Closing(ByVal sender As Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles MyBase.Closing
        If MessageBox.Show("Do you want to exit now?", "Exit application", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = DialogResult.No Then
            e.Cancel = True
        End If
    End Sub
 
Back
Top