7 Replies - 7937 Views - Last Post: 04 August 2009 - 08:09 PM Rate Topic: -----

#1 rmiller1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-December 07

MDI Child Problems in VB.net 2008

Posted 01 August 2009 - 07:58 AM

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!


 
' 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


Is This A Good Question/Topic? 0
  • +

Replies To: MDI Child Problems in VB.net 2008

#2 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: MDI Child Problems in VB.net 2008

Posted 01 August 2009 - 08:14 AM

Try modifying this line.
  Dim ChildForm As New System.Windows.Forms.Form


to
  Dim WithEvents ChildForm As New System.Windows.Forms.Form



to see if the closing event is trigger on the MDIChild
Was This Post Helpful? 0
  • +
  • -

#3 LoveIsNull  Icon User is offline

  • Recovering D.I.C Addict
  • member icon

Reputation: 52
  • View blog
  • Posts: 646
  • Joined: 10-March 09

Re: MDI Child Problems in VB.net 2008

Posted 01 August 2009 - 10:47 AM

I know someone just made a thread about this recently, not sure if it was you but anways.
You need to use an AddHandler statement to link the event to that method manually if the control it is not declared WithEvents (See AdamSpeight's example). But, also note that you cannot declare a local variable WithEvents, you would have to do so at the class level and I don't know how well that works with dynamic forms.
'Before you show the form
ChildForm.Text = "Question " & m_ChildFormNumber
AddHandler ChildForm.FormClosing, AddressOf Me.Child_Form_template_FormClosing
ChildForm.Show()


Was This Post Helpful? 1
  • +
  • -

#4 rmiller1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-December 07

Re: MDI Child Problems in VB.net 2008

Posted 01 August 2009 - 08:28 PM

The only problem i have with the following is that instead of MsgBoxResult.Yes or MsgBoxResult.Cancel i am returned with a -1 for yes and a 0 for no and cancel.

View PostLoveIsNull, on 1 Aug, 2009 - 09:47 AM, said:

I know someone just made a thread about this recently, not sure if it was you but anways.
You need to use an AddHandler statement to link the event to that method manually if the control it is not declared WithEvents (See AdamSpeight's example). But, also note that you cannot declare a local variable WithEvents, you would have to do so at the class level and I don't know how well that works with dynamic forms.
'Before you show the form
ChildForm.Text = "Question " & m_ChildFormNumber
AddHandler ChildForm.FormClosing, AddressOf Me.Child_Form_template_FormClosing
ChildForm.Show()






Unfortunately the withevents did not solve the problem still getting the error: Cannot access a disposed object. Object name: 'Form'.

View PostAdamSpeight2008, on 1 Aug, 2009 - 07:14 AM, said:

Try modifying this line.
  Dim ChildForm As New System.Windows.Forms.Form


to
  Dim WithEvents ChildForm As New System.Windows.Forms.Form



to see if the closing event is trigger on the MDIChild

Was This Post Helpful? 0
  • +
  • -

#5 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: MDI Child Problems in VB.net 2008

Posted 01 August 2009 - 09:37 PM

When you close a Form .net thinks you finish using it, so it disposes of it.
Once disposed it no longer exists.
So when it try to reopen a reference to it, it crashes.
Was This Post Helpful? 0
  • +
  • -

#6 rmiller1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-December 07

Re: MDI Child Problems in VB.net 2008

Posted 01 August 2009 - 10:24 PM

View PostAdamSpeight2008, on 1 Aug, 2009 - 08:37 PM, said:

When you close a Form .net thinks you finish using it, so it disposes of it.
Once disposed it no longer exists.
So when it try to reopen a reference to it, it crashes.



I am thinking that what you are trying to tell me with the mycup of tea is that i need to put all the properties into the code and create a new form, if i am incorrect please tell me because the whole reason for the child form template was so that i would not have to do that, i was trying to just use it as a reference and get all my properties and code from it! If i am correct then i have alot of coding to do.
Was This Post Helpful? 0
  • +
  • -

#7 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2241
  • View blog
  • Posts: 9,412
  • Joined: 29-May 08

Re: MDI Child Problems in VB.net 2008

Posted 01 August 2009 - 10:55 PM

Firstly MyCup(Of T) code is my signature.

It possible to create multiple new instance of the same Form.

In the MDI Parent
Declare a private list of ChildFormTemplates.
Private WithEvent MyChildForms As New List(Of ChildFormTemplate)



In the MdiParent have some way of creating a new Form (say a Button)
MyChildForms.Add(New ChildFormTemplate)
' add form ito MDIParent.



Add a new sub to the Mdi Parent.
 Public Sub RemoveChild(ByRef cf As ChildFormTemplate)
  If MyChildForms.contains(cf) Then
   MyChildForms.Remove(cf)
  EndIf



In MDI Child's closing Event call the RemoveChild Sub
{MDIParent>} .RemoveChild(Me)


Was This Post Helpful? 0
  • +
  • -

#8 rmiller1981  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 06-December 07

Re: MDI Child Problems in VB.net 2008

Posted 04 August 2009 - 08:09 PM

Thank you all for Your input and your suggestions, I have resolved my issues with your help and the help of everyone at this post: http://www.vbdotnetf...html#post105109
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1