form.cancelbutton property issues

  • Thread starter Thread starter Nigel V Thomas
  • Start date Start date
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
 
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.
 
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.
 
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
 
Back
Top