Confirm Exit in a Mdi form?

I

iDesmet

Hallo,

I have a Mdi form in which I show a MessageBox if the user tries to
close it. If the user clicks YES, then the form closes; if it clicks
NO then it aborts.

I added the following lines in my form Closing event:

If MessageBox.Show("Are you sure do you want to exit?", gsAppName,
MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If

But it doesn't work. So I was wondering if someone is so kind to point
me to the error.

I'm using VB.NET

Thanks in advance.

PS My apologies for my bad english.

Best Regards,
David Desmet
 
A

Armin Zingler

iDesmet said:
Hallo,

I have a Mdi form in which I show a MessageBox if the user tries to
close it. If the user clicks YES, then the form closes; if it clicks
NO then it aborts.

I added the following lines in my form Closing event:

If MessageBox.Show("Are you sure do you want to exit?", gsAppName,
MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes
Then Me.Close()
End If

But it doesn't work. So I was wondering if someone is so kind to
point me to the error.


If MessageBox.Show("Are you sure do you want to exit?", gsAppName, _
MessageBoxButtons.YesNo, MessageBoxIcon.Question, _
MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.No
Then
e.cancel = True
End If

Note: DialogResult.Yes changed to DialogResult.No.

You don't have to (or must not) call Me.Close because it must have been
called before already; otherwise the FormClosing event would not have been
fired.


Armin
 
K

kimiraikkonen

Hallo,

I have a Mdi form in which I show a MessageBox if the user tries to
close it. If the user clicks YES, then the form closes; if it clicks
NO then it aborts.

I added the following lines in my form Closing event:

If MessageBox.Show("Are you sure do you want to exit?", gsAppName,
MessageBoxButtons.YesNo, MessageBoxIcon.Question,
MessageBoxDefaultButton.Button2) = Windows.Forms.DialogResult.Yes Then
Me.Close()
End If

But it doesn't work. So I was wondering if someone is so kind to point
me to the error.

I'm using VB.NET

Thanks in advance.

PS My apologies for my bad english.

Best Regards,
David Desmet

David,
Try this one:

Private Sub Form1_Closing(ByVal sender As System.Object, ByVal e As
System.ComponentModel.CancelEventArgs) Handles MyBase.Closing

If MessageBox.Show("Are you sure do you want to exit?",
"gsAppName",MessageBoxButtons.YesNo,
MessageBoxIcon.Question,MessageBoxDefaultButton.Button2) =
Windows.Forms.DialogResult.Yes Then
Application.Exit()
Else
e.Cancel = True
End If
End Sub

Thanks,

Onur Güzel
 
C

Cor Ligthert [MVP]

David,

Just from my memory.

DirectCast(me.MDIParent,TheMainForm).Close()

Would do the trick

(I am not so busy with all the kind of tricks in VBNet for VB6 users, so
maybe it can be done with less code)

Cor
 
I

iDesmet

Thank you all for the kind help and fast reply... this has helped me
very much.

I appreciate your time.

Thanks,
David Desmet
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top