Results 1 to 17 of 17

Thread: [RESOLVED] Multiple forms/parent/child forms. Whats the correct way?

  1. #1

    Thread Starter
    Member Cyrax.NET's Avatar
    Join Date
    Feb 2007
    Posts
    54

    Resolved [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
    Last edited by Cyrax.NET; Mar 18th, 2009 at 11:57 AM.

  2. #2
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  3. #3

    Thread Starter
    Member Cyrax.NET's Avatar
    Join Date
    Feb 2007
    Posts
    54

    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.

  4. #4
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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?
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  5. #5

    Thread Starter
    Member Cyrax.NET's Avatar
    Join Date
    Feb 2007
    Posts
    54

    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

  6. #6

    Thread Starter
    Member Cyrax.NET's Avatar
    Join Date
    Feb 2007
    Posts
    54

    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?

    Thanks in advance

  7. #7
    Lively Member
    Join Date
    May 2008
    Posts
    75

    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

  8. #8
    Lively Member
    Join Date
    May 2008
    Posts
    75

    Re: Multiple forms/parent/child forms. Whats the correct way?

    k, i have a sample app for you.,
    are u using vs 2008?

  9. #9

    Thread Starter
    Member Cyrax.NET's Avatar
    Join Date
    Feb 2007
    Posts
    54

    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.

  10. #10
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Multiple forms/parent/child forms. Whats the correct way?

    Quote Originally Posted by zxed View Post
    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!!

  11. #11
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    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.

    Name:  Form1.png
Views: 866
Size:  125.3 KB

    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()
    Last edited by Jenner; Mar 18th, 2009 at 08:55 AM.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  12. #12
    Lively Member
    Join Date
    May 2008
    Posts
    75

    Re: Multiple forms/parent/child forms. Whats the correct way?

    Quote Originally Posted by gep13 View Post
    I am not sure I agree with the above!!
    U don't have 2,. It was humor

  13. #13
    Lively Member
    Join Date
    May 2008
    Posts
    75

    Re: Multiple forms/parent/child forms. Whats the correct way?


  14. #14

    Thread Starter
    Member Cyrax.NET's Avatar
    Join Date
    Feb 2007
    Posts
    54

    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

    Code:
    MenuStrip1.Focus()
    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

  15. #15
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED] Multiple forms/parent/child forms. Whats the correct way?

    Nice! Glad you got it working!
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  16. #16
    Addicted Member
    Join Date
    Sep 2008
    Posts
    183

    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

  17. #17
    Lively Member
    Join Date
    May 2008
    Posts
    75

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width