MDI Child Problems

rmiller1981

New member
Joined
Aug 1, 2009
Messages
4
Programming Experience
1-3
I have a MDI based application
with two problems.

First: when i close the MDI child i want to ask if the user would like to save the data in the child and give them the regular choice yes,no,cancel however when i try to use the Formclosing event it never fires, it does fire for the mdi parent but that is no help.

Second: When i load the child i am using a template form to get all the controls and code, but if i close all the children then reopen one it errors because the controls are disposed.

Can anyone help solve these problems? I am including some of the code i am having errors with!

VB.NET:
' The code to create the new child forms:

Private m_ChildFormNumber As Integer
    Dim new_controls(Child_Form_template.Controls.Count) As Control
    Private Sub ShowNewForm(ByVal sender As Object, ByVal e As EventArgs) Handles NewToolStripMenuItem.Click, NewToolStripButton.Click, NewWindowToolStripMenuItem.Click
        ' Create a new instance of the child form.
        Dim ChildForm As New System.Windows.Forms.Form
        ' set the layout of the child form.
        ChildForm.Controls.AddRange(new_controls)
        ChildForm.CausesValidation = True
        ' Make it a child of this MDI form before showing it.
        ChildForm.MdiParent = Me
        m_ChildFormNumber += 1
        ChildForm.Text = "Question " & m_ChildFormNumber
        ChildForm.Show()
    End Sub
    Private Sub Main_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Child_Form_template.Controls.CopyTo(new_controls, 0)

    End Sub

' The code for the formclosing event:

Private Sub Child_Form_template_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
        If saved_status = 0 Then
            Dim save_now As MsgBoxResult = MsgBox("Question not saved, Would you like to save now?", MsgBoxStyle.YesNoCancel, "Save Question?") = MsgBoxResult.Yes
            If save_now = MsgBoxResult.Yes Then
                Save_Question()
            ElseIf save_now = MsgBoxResult.Cancel Then
                e.Cancel = True
            End If
        End If
    End Sub
 
In your form closing sub remove the handles clause, then where you create the child form use:
VB.NET:
Addhandler ChildForm.FormClosing, AddressOf Child_Form_template_FormClosing
 
Almost there!

The only problem i have with this is that instead of MsgBoxResult.Yes or MsgBoxResult.Cancel i am returned with a -1 for yes and a 0 for no and cancel.
 
I would use Messagebox, but this same format works...
VB.NET:
If MessageBox.Show("something", "SaveThis", MessageBoxButtons.YesNoCancel) = DialogResult.Ok Then
      'do something...
      MessageBox.Show("Saved", "Done", MessageBoxButtons.OK)
ElseIf DialogResult.Cancel Then
      'do something else...
End If
 
One down

Ok i was able to resolve this issue modifing the last post a little bit here is the answer!

Public Sub Child_Form_template_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) ' Handles Me.FormClosing
If saved_status = 0 Then
Dim save_now As DialogResult = DialogResult.OK
save_now = MessageBox.Show("Question not saved, Would you like to save now?", "Save Question?", MessageBoxButtons.YesNoCancel)
If save_now = DialogResult.Yes Then
Save_Question()
ElseIf save_now = DialogResult.Cancel Then
e.Cancel = True
End If
End If
End Sub

As for the disposed thing i still have yet to fix that!
 
It would be easier to make a usercontrol that holds all these controls, then when you make a new child form you make a new instance of this and add it to your new child form. This would solve the disposed controls problem.
 
All problems Resolved

Ok Taking the advise of everyone here i have created a new user control to hold all the controls that i needed and just used that in my application. This made things so much easier because now if i need to change any thing i just change the user control and update the application with that, plus when i want to create a child form i simply add one control to that form and tada. I am done!

Before asking this question and reading your responses i had never done anything like this before, nor did i even know how to do it. But after getting your advise (and reading this how to Visible Objects: Creating Your First User Control) I have successfully created, tested, and implemented this.

Thank you all very much for helping me with these problems and to better my skills.
 
Back
Top