form.cancelbutton property issues

N

Nigel V Thomas

Hi, I have what would seem a simple issue but I can't get to the bottom of it.

I have a data entry form shown as a .ShowDialog.
It has two buttons (Accept and Cancel)
Form.CancelButton is set to the Cancel button
Form.AcceptButton is set to the Accept button

All worked fine until I decided to add a dialog to the Cancel button's event
if the data has changed such as.

msgbox("Save changes Yes/No/Cancel",YesNoCancel)

Now, if I trap the Cancel MsgBox result the form still closes. It seems that
the form.CancelButton is closing the form. How can I stop the form from
closing even if I have set the Form.CancelButton?

Cheers
 
A

Armin Zingler

Nigel said:
Hi, I have what would seem a simple issue but I can't get to the bottom of it.

I have a data entry form shown as a .ShowDialog.
It has two buttons (Accept and Cancel)
Form.CancelButton is set to the Cancel button
Form.AcceptButton is set to the Accept button

All worked fine until I decided to add a dialog to the Cancel button's event
if the data has changed such as.

msgbox("Save changes Yes/No/Cancel",YesNoCancel)

Now, if I trap the Cancel MsgBox result the form still closes. It seems that
the form.CancelButton is closing the form. How can I stop the form from
closing even if I have set the Form.CancelButton?

Handle the FormClosing event. Set e.cancel = true if the Form has to stay open.

Another way is changing the Form's DialogResult value inside the Button's click
event. If you set the Form's CancelButton property, the same Button's DialogResult
property is set to 'Cancel'. As soon as you click a Button that's DialogResult property
is <> None, the Form's DialogResult property is set to the same value. This again
causes the Form to be closed. Consequently, pressing the Button closes the Form.

If pressing the button does not necessarily close the Form, I'd set the Button's
DialogResult property to None and explicitly set the Form's DialogResult property
inside the Button's click event to Cancel depending on the selection made in
the msgbox.
 
A

Armin Zingler

Armin said:
If you set the Form's CancelButton property, the same Button's DialogResult
property is set to 'Cancel'.

That's done at design time by the designer, not at run time.
 
A

Armin Zingler

Armin said:
That's done at design time by the designer, not at run time.

Sry, me again...
I was wrong. I've always thought it's a feature of the Form designer that
setting the Cancel/AcceptButton property also changes the DialogResult
property of the assigned button. However, it is not. It's built into the
Framework, so if you write this

Private Sub Form1_Load( _
ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

Debug.Print(Button2.DialogResult.ToString)
CancelButton = Button2
Debug.Print(Button2.DialogResult.ToString)

End Sub

the output is:

None
Cancel
 

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