DialogResult woes...

  • Thread starter Thread starter Nigel V Thomas
  • Start date Start date
N

Nigel V Thomas

I'm having issues with form.DialogResult - here's what I am trying to do:

I have a form with 3 exit points:
1. OK Button (sets DialogResult to .OK)
2. Cancel Button (sets DialogResult to .Cancel)
3. Form close button which queries user (in the Form_FormClosing event) if
changes have been made (MsgBox and Yes/No/Cancel buttons.) where...
Selecting Yes sets DialogResult to .OK
Selecting No sets DialogResult to .Cancel
Selecting Cancel cancels the unloading of the form

Here's the issue:
The OK and Cancel buttons work fine, however, if I Close the form and select
Cancel (thus cancelling the FormClose event) and then subsequently hit the OK
or Cancel button, I get a different CloseReason (3) in the FormClosing Event

Can anyone suggest a reason, or should I be closing the form a different way?


Nigel
 
Nigel said:
I'm having issues with form.DialogResult - here's what I am trying to do:

I have a form with 3 exit points:
1. OK Button (sets DialogResult to .OK)
2. Cancel Button (sets DialogResult to .Cancel)
3. Form close button which queries user (in the Form_FormClosing event) if
changes have been made (MsgBox and Yes/No/Cancel buttons.) where...
Selecting Yes sets DialogResult to .OK
Selecting No sets DialogResult to .Cancel
Selecting Cancel cancels the unloading of the form

Here's the issue:
The OK and Cancel buttons work fine, however, if I Close the form and select
Cancel (thus cancelling the FormClose event) and then subsequently hit the OK
or Cancel button, I get a different CloseReason (3) in the FormClosing Event

Can anyone suggest a reason, or should I be closing the form a different way?


Nigel

What value are you getting in the FormClosing event handler when your OK
or Cancel buttons are hit? UserClosing would be expected to me.

A way around this is to set a flag at the form level, that changed to
true by the OK and Cancel handlers. Check the flag in the FormClosing
event to see if the messagebox is necessary.
 
Family Tree Mike said:
What value are you getting in the FormClosing event handler when your OK
or Cancel buttons are hit? UserClosing would be expected to me.

A way around this is to set a flag at the form level, that changed to
true by the OK and Cancel handlers. Check the flag in the FormClosing
event to see if the messagebox is necessary.

Thanks for the reply Mike

I'm setting the DialogResult as you would expect (OK and Cancel)

It would seem that the DialogResult does not work as expected and flagging
it is the way forward - typical. Why introduce a feature which does not work!

Nigel
 
Nigel said:
I'm having issues with form.DialogResult - here's what I am trying to do:

I have a form with 3 exit points:
1. OK Button (sets DialogResult to .OK)
2. Cancel Button (sets DialogResult to .Cancel)
3. Form close button which queries user (in the Form_FormClosing event) if
changes have been made (MsgBox and Yes/No/Cancel buttons.) where...
Selecting Yes sets DialogResult to .OK
Selecting No sets DialogResult to .Cancel
Selecting Cancel cancels the unloading of the form

Here's the issue:
The OK and Cancel buttons work fine, however, if I Close the form and select
Cancel (thus cancelling the FormClose event) and then subsequently hit the OK
^^^^^^^^^ FormClosing or FormClosed?
or Cancel button, I get a different CloseReason (3) in the FormClosing Event

Can anyone suggest a reason, or should I be closing the form a different way?

I tried to repro the problem. I always get 'UserClosing' in e.CloseReason
(in FormClosing and in FormClosed event)

What's your code in the "Form close button"'s click event? From your statement above
("Form close button which queries user (in the Form_FormClosing event)") it's not
clear to me if the query is inside FormClosing or in the close button's click event
handler.
 
Armin Zingler said:
^^^^^^^^^ FormClosing or FormClosed?


I tried to repro the problem. I always get 'UserClosing' in e.CloseReason
(in FormClosing and in FormClosed event)

What's your code in the "Form close button"'s click event? From your statement above
("Form close button which queries user (in the Form_FormClosing event)") it's not
clear to me if the query is inside FormClosing or in the close button's click event
handler.

Armin, thanks for the reply...

The two buttons (OK and CANCEL) set DialogResult to OK and CANCEL - both
these work fine - no problems.

The Form_Closing event contains the following code:


Private Sub frmCommon_Input_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
Do
'Call MsgBox("Close reason " & e.CloseReason)

'Form's changes have been delt with. Form is being disposed by calling
module
If Me.Changed = False Then 'If e.CloseReason = CloseReason.None Then
Exit Do
End If

'Being closed by calling routine, saving and extraction of data will have
already occured
If e.CloseReason = 0 Then
Exit Do
End If

'Something else is closing form (Application/Windows/User)
Select Case FN_pOKToClose()

Case OKToCloseResult.CloseFormSaveChanges
If Validate() = True Then
Call pExit_Save()
Else
e.Cancel = True
End If

Case OKToCloseResult.CloseFormLooseChanges
Call pexit_Cancel()

Case OKToCloseResult.CancelCloseForm
e.Cancel = True

End Select

Exit Do
Loop
End Sub


The routine FN_pOKToClose simply returns Yes/No/Cancel after testing if data
has changed.



Nigel
 
Armin Zingler said:
^^^^^^^^^ FormClosing or FormClosed?


I tried to repro the problem. I always get 'UserClosing' in e.CloseReason
(in FormClosing and in FormClosed event)

What's your code in the "Form close button"'s click event? From your statement above
("Form close button which queries user (in the Form_FormClosing event)") it's not
clear to me if the query is inside FormClosing or in the close button's click event
handler.

Armin, here's the code...

Add a form to your solution and two buttons ButtonOK and ButtonCancel then
add the code below...





Public Class Form1

Sub New()

' This call is required by the Windows Form Designer.
InitializeComponent()

' Add any initialization after the InitializeComponent() call.

End Sub


Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As
System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing

Call MsgBox("Close reason" & e.CloseReason)

If e.CloseReason = CloseReason.UserClosing Then

Select Case MsgBox("Save changes", MsgBoxStyle.YesNoCancel)
Case MsgBoxResult.Yes
DialogResult = Windows.Forms.DialogResult.Yes
Case MsgBoxResult.No
DialogResult = Windows.Forms.DialogResult.No
Case MsgBoxResult.Cancel
DialogResult = Windows.Forms.DialogResult.Cancel
e.Cancel = True
End Select

End If


End Sub


Private Sub ButtonCancel_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonCancel.Click
Me.DialogResult = Windows.Forms.DialogResult.Cancel
End Sub

Private Sub ButtonOK_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles ButtonOK.Click
Me.DialogResult = Windows.Forms.DialogResult.OK
End Sub

End Class



Try this:

Run and Click OK and you'll get result = 0 Correct: Form wil
Run again and click Cancel, and you'll get result = 0 Correct: Form wil
Run again and close the form and you'll get result = 3 (click Yes or No)
Correct: Form wil Close

Now, try this.

Run and close the form but this tiime click Cancel. Result will be 3;
Correct and form will remain open. Now click OK or Cancel and result will be
3 whereas in the previous test it was 0

What's going on???

Nigel
 
Nigel said:
If e.CloseReason = 0 Then

If e.CloseReason = CloseReason.None Then

gives me more information. :-)


The Do-Loop doesn't really make sense, does it?

Well, after completing your code to have a compilable example, I've
logged e.CloseReason but still get "UserClosing" every time.
 
Nigel said:
Try this:

Run and Click OK and you'll get result = 0 Correct: Form wil
Run again and click Cancel, and you'll get result = 0 Correct: Form wil
Run again and close the form and you'll get result = 3 (click Yes or No)
Correct: Form wil Close

Now, try this.

Run and close the form but this tiime click Cancel. Result will be 3;
Correct and form will remain open. Now click OK or Cancel and result will be
3 whereas in the previous test it was 0

What's going on???

Now I see the problem. If the Form is closed by setting the DialogResult,
e.CloseReason is "None". If an attempt to close it has been made before
but closing has been cancelled, e.closeReason is "UserClosing" this time.

Looks like a bug. It seems the CloseReason is not beeing reset if closing
is cancelled. I've searched in the feedback database
http://connect.microsoft.com/VisualStudio/SearchResults.aspx?SearchQuery=CloseReason

I don't see exactly this issuse mentioned. If nobody else has a good explanation
or tells us it's "by design", I suggest you report the problem there.
 
Now I see the problem. If the Form is closed by setting the DialogResult,
e.CloseReason is "None". If an attempt to close it has been made before
but closing has been cancelled, e.closeReason is "UserClosing" this time.

Looks like a bug. It seems the CloseReason is not beeing reset if closing
is cancelled. I've searched in the feedback database
http://connect.microsoft.com/VisualStudio/SearchResults.aspx?SearchQuery=CloseReason

I don't see exactly this issuse mentioned. If nobody else has a good explanation
or tells us it's "by design", I suggest you report the problem there.
Armin

Thanks for your time and effort on this one. Yup, it certianly looks like a
bug.

Cheers

Nigel
 
Back
Top