[RESOLVED] Multiple forms/parent/child forms. Whats the correct way?
Hello and thanks for reading.
Am devloping an application with multiple forms, which reside on top of a main form. i.e the main application window. The 'child' forms do not need to be linked in any way as they do not pass information to the parent form, as all the parent form does is act as main window.
Now I would like to know what is the correct way for the 'main window' form acting as a container for the other forms? So that all the smaller secondary forms stay focused on top of the main application form, that when the main window is minimized, so these forms should do also etc..
I have read other posts and they mention, leaving all the forms standard i.e. IsMdiContainer = False and then using form.showDialog() to call the secondary forms. This did not work for me as what happens is that as soon as I give focus to the main app form, the secondary form disappears underneath.
I tired making the main app form IsMdiContainer = True and when it loads calling:
Code:
frmMemberForm.MdiParent = Me
Now this works, because I can click off the form and minimize the app etc and the frmMemberForm doesn't disappear. However if i close the MemberForm and reopen it, it looses this property and once again when it looses focus, it will disappear.
Can someone tell me what is the correct way of sorting this out please?
Thanks in advance
Re: Multiple forms/parent/child forms. Whats the correct way?
You should be making new instances of your forms:
Code:
Dim f As New frmMemberForm
f.MdiParent = Me
f.Show()
You need this call whenever you want to pop up a new Child Form. Closing it will dispose of it and yes, it'll loose all properties.
Forms are nothing more than objects, i.e. "classes" in .NET. Thus, you can conjure up as many as you want and destroy them again after using them.
Re: Multiple forms/parent/child forms. Whats the correct way?
jenner thanks for replying!
i didn't realise you needed to create new instances of forms, they alwasys seemed like 'special' objects all already defined in .net but ill start looking at them in different light.
i tried what you mentioned but it still didn't work.
this is what i'm doing, perhaps you can see my error:
frmMemberForm is ordinary form. In my main window frm I have set IsMdiContainer = True. At top of class outside any method etc i declare
Code:
Dim f As New frmMemberForm
Then in the button even, i put rest of code to set mdi parent and show.
Code:
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
f.MdiParent = Me
f.Show()
End Sub
Still same problem, when frmMemberForm looses focus, it disappears. I also tried making the main app IsMdiContainer = False, still no luck, i also tried doing f.show(me), still no luck....
Anything other idea please?
thanks.
Re: Multiple forms/parent/child forms. Whats the correct way?
Move the "Dim f As New frmMemberForm" into your button event.
Also, do you only ever want one of these forms to come up at a time?
Re: Multiple forms/parent/child forms. Whats the correct way?
Jenner,
It makes sense of course, to create a new instance of the form and set the main window as its parent everytime the button is pressed, because once you close the form, that instance is disposed. Bringing back some OO concepts slowly! :)
However it still didn't work. Again with the main window IsMdiContainer = True and False.
Your code looks like it should work but dont understand why its not.
Could it be the .Me instance am using for mdiparent = me? Does the 'me' mean the calling object or the form that is active at the time of the call?
Also you ask "do you only ever want one of these forms to come up at a time?"
At moment am not sure actually but please explain how this would affect things?
Thanks again
Re: Multiple forms/parent/child forms. Whats the correct way?
Does anyone else have any suggestions?
I'm suprised this is prooving to be tricky, as what I'm trying to acheive is a basic feature of any application...for the subforms to be childern of the main app? :confused:
Thanks in advance :thumb:
Re: Multiple forms/parent/child forms. Whats the correct way?
first of all., the correct way to do anything., is "any" way, that gets it going :).,
the main form that you are to make the midi parent., have you set the type correctly? give me a few minutes and ill try to get you sample code/app
Re: Multiple forms/parent/child forms. Whats the correct way?
k, i have a sample app for you.,
are u using vs 2008?
Re: Multiple forms/parent/child forms. Whats the correct way?
hi zxed
yes im using VS2008 SP1 .net 3.5 SP1
how do you mean, have i set the type correctly?
yes please if you have a sample app that will fix this problem for me, then let me know...i need to finish the project and this is only thing holding me back!
thanks.
Re: Multiple forms/parent/child forms. Whats the correct way?
Quote:
Originally Posted by
zxed
first of all., the correct way to do anything., is "any" way, that gets it going :).,
I am not sure I agree with the above!!
1 Attachment(s)
Re: Multiple forms/parent/child forms. Whats the correct way?
Ok, here's the step-by-step.
I made a new project and made 2 forms. Form1 and Form2.
Form1 is going to be my MdiParent so the only change I made on it was:
IsMdiContainer = True
I also added a panel, docked it left, and a button to pop up my second form.
You can see the result below.
Attachment 69818
Now, I add my button code. I want to do this a little fancy, so I added a bit to restrict it so only one Form2 is up at a time. Basically, if there's a Form2 already on-screen, just select it to bring it forward. Otherwise, make a new one.
Code:
For Each fChildren As Form In MdiChildren
If TypeOf fChildren Is Form2 Then
fChildren.Select()
Return
End If
Next
Dim f As New Form2
f.MdiParent = Me
f.Show()
Re: Multiple forms/parent/child forms. Whats the correct way?
Quote:
Originally Posted by
gep13
I am not sure I agree with the above!!
U don't have 2,. It was humor
Re: Multiple forms/parent/child forms. Whats the correct way?
Re: Multiple forms/parent/child forms. Whats the correct way?
Jenner,
thank you loads, this problem was driving me mad. I dont usually ask for help unless am stick.
My original code is working as it should with the mdi container and parent but you posting the new project got me thinking of testing from scratch again.
Guess what the problem was...nothing to do with containers, nothing about form properties, nothing else but I had
when the main app loads...(my app uses menustrip instead of buttons)...
Not quite sure why focusing the menu strip before loading the forms was causing it not to work. Perhaps it because its passing control to the menu strip which then becomes the parent???
Anyways point is, its working.
Cheers :)
Re: [RESOLVED] Multiple forms/parent/child forms. Whats the correct way?
Nice! Glad you got it working!
Re: [RESOLVED] Multiple forms/parent/child forms. Whats the correct way?
I have a related issue which keeps kicking back an object disposed error. I have one main form with multiple sub-forms selected from a navigation bar. Most of the sub-forms have different names so the code below works fine. However, some were so similar, that I reused the same form (hiding certain text boxes and labels).
The result is that when you move from one form to the other, the software sees that they're both the same name and I get "Cannot access a disposed object"
Code:
Private Sub openForm(ByVal formName As Form, ByVal Title As String) Handles navPanel.LabelClicked
If pnlMainContainer.Controls.Count > 0 Then
Dim tempForm As Form = pnlMainContainer.Controls(0)
tempForm.Close()
End If
formName.TopLevel = False
formName.Parent = pnlMainContainer
formName.Show()
End Sub
Re: [RESOLVED] Multiple forms/parent/child forms. Whats the correct way?
No. "Cannot access a disposed object" means what it states... the object was disposed.. make sure the form you are trying to show. was/is open.