Is there a Standard place for this logic?

  • Thread starter Thread starter Charles Law
  • Start date Start date
C

Charles Law

In an MDI app, I want to warn users when they attempt to close any of the
MDI child windows, and give them the option to cancel the close. I put code
in the Closing event of the child form to display a prompt.

The problem is, when I close the application, the prompt is displayed for
every child window as it closes, although the warning is not appropriate at
this time.

I thought it would be possible to determine why a form was closing, e.g.
because Windows is shutting down, or the user clicked the close button, etc.

Is there a better/more appropriate way to do this? I am reluctant to start
including parameters that get set and reset depending on who clicked what
and when.

TIA

Charles
 
Charles,

The best way to do it is to use a boolean variable within your app that will
receive true if the application is being closed and check for that variable
on the Closing event handler for your child windows. Then, you only dispay
your mmessage when that variable is not true. Does that make sens to you?

Telmo Sampaio
 
Hi Telmo

Thanks for the response. I understand what you are suggesting, but was
hoping to avoid an application variable that I have to maintain manually.

There used to be a way of finding out why a window was closing (VB6, and
before), but I can't find it now. I feel that wherever it turns up is
probably the place to implement some additional logic.

Charles
 
Hi Charles,

Does this code help you, I sand it to Stan yesterday and the suprissing
thing is that I can use it in a more simple way myself now

I have used it now in the mdi.parent, with the thread from Stan you can see
how it is used from a MDIchildform

\\\
Dim frmExist As Boolean
For Each frm As Form In Me.MdiChildren
If frm.Name = "Pers" Then
frmExist = True
End If
Next
If Not frmExist Then
Dim frmNew As New frmPers
frmNew.MdiParent = Me
frmNew.Show()
frmNew.Name = "Pers"
frmNew.BringToFront()
End If
///
Cor
 
Hi Cor

Is this an answer to another question? It doesn't seem to follow on from the
current thread.

Charles
 
Hi Charles,

No it was more that I got the idea that you was building more and more logic
to prevent the user to close a MDI form. With this it is maybe less needed.

Cor
 
Hi Cor

It is not so much that I want to prevent them from closing, but warn them of
the consequence of closing, that is, the contents of the form will be lost.

However, if the application is closing anyway, then it doesn't matter.

Charles
 
Charles,

I did some testing using the sender parameter for the MDI child form in the
Closing event, but that will not help either. The sender is the child object
even when we close the Parent form. I will do some research later to see if
we can use the Call Stack to see where the closing came from before hitting
the child form, but I think the only solution will be a Parent Form
Variable.

Telmo
 
* "Charles Law said:
It is not so much that I want to prevent them from closing, but warn them of
the consequence of closing, that is, the contents of the form will be lost.

However, if the application is closing anyway, then it doesn't matter.

You could add a handler to the form's 'Closing' event:

\\\
Private Sub Form1_Closing( _
ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs _
) Handles MyBase.Closing
If _
MessageBox.Show( _
"Really close?", _
Application.ProductName, _
MessageBoxButtons.YesNo, _
MessageBoxIcon.Question _
) = DialogResult.No _
Then
e.Cancel = True
End If
End Sub
///
 
Hi Herfried

I think this is where we came in. I can handle the Closing event, but I
don't always want to display the message.

If the user clicks the Close button on the MDI child form then I want to
display the message (for the child form). If they click the Close button on
the MDI parent form then I don't want to display the message for the child
forms. It is preferable to determine which is which without having to resort
to a manually maintained class variable.

Why did Microsoft do away with the parameter that indicated the source of
the close, do you suppose?

Charles
 
* "Charles Law said:
I think this is where we came in. I can handle the Closing event, but I
don't always want to display the message.

If the user clicks the Close button on the MDI child form then I want to
display the message (for the child form). If they click the Close button on
the MDI parent form then I don't want to display the message for the child
forms. It is preferable to determine which is which without having to resort
to a manually maintained class variable.

You could add a Boolean property to the child form and set this child
form if the 'Closing' event of the container is fired. Then you can
display the message only if the property is not set.
Why did Microsoft do away with the parameter that indicated the source of
the close, do you suppose?

Who knows...
 
Hi Charles,

I hope this is not to late, just a sample you have to tune it to your needs.

I hope this helps?

Cor
\\\
Public WithEvents B As New Form3
Public WithEvents A As New Form2
Private Sub Form1_Load(ByVal sender As Object, ByVal e _
As System.EventArgs) Handles MyBase.Load
Dim myforms() As Form = New Form() {A, B}
A.MdiParent = Me
B.MdiParent = Me
A.Show()
B.Show()
For Each frm As Form In myforms
AddHandler frm.Closing, AddressOf frm_Closing
Next
End Sub
Private Sub frm_Closing(ByVal sender As Object, _
ByVal e As System.ComponentModel.CancelEventArgs)
If Not MessageBox.Show("close", "", _
MessageBoxButtons.YesNo) = DialogResult.Yes Then
e.Cancel = True
End If
End Sub
///
 
Back
Top