Results 1 to 16 of 16

Thread: Mdi Problem

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Mdi Problem

    I am writing an MDI app in .NET.

    I want to add buttons to the MDI parent form which show child forms when clicked. I want the child forms to merge with the parent so that the parent's main menu items are still visible.

    I have 2 problems:

    1.When I show a child form using:

    VB Code:
    1. Dim Form1 As New Form1
    2.  
    3.         Form1.MdiParent = Me
    4.         Form1.Show()

    you can still see and access the buttons on the parent form even if I add Form1 .BringToFront()

    If I remove the line Form1 .MdiParent = Me, the parent form's menu is no longer visible. ie. the child is not contained within the parent.

    2.When I click on a button that shows a child form, it creates a new instance of the form each time. How do I activate a previous instance of the form if there is one rather than creating a new instance?

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    I don't understand what you are trying to do can you explain further?

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374
    OK. I have a parent MDI form called frmMain (IsMDIContainer=True)

    frmMain has buttons on it eg.Button1 which shows Form1

    VB Code:
    1. Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    2. Dim Form1 As New Form1
    3.  
    4.         Form1.MdiParent = Me
    5.         Form1.Show()
    6.  
    7.  
    8.  
    9.     End Sub

    When I click on Button1, Form1 appears OK but the buttons on frmMain are still visible and accessible and any controls in the same position on Form1 are hidden and are not accessible.

    I have solved part 2 of the problem by removing:

    VB Code:
    1. Dim Form1 As New Form1
    from the button click procedure and declaring it at form level. Therefore, only one instance of the form is created and when you call the show method, that instance is shown rather than a new instance.

  4. #4
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Anything on the MDIParent superceeds the child so you'll need to manually change the Visible property of the things you don't want to appear on the main form once the child is shown.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374
    The solution might be to have a separate main menu form which is a child form itself.

    I thought I had solved the second problem by declaring the form at form level. This worked until I closed the form and it wouldn't let me re-open it.

    "Cannot access a disposed object named Form1"

    I tried adding:

    If IsNothing(Form1) Then Form1= New Form1

    to the button click procedure but IsNothing(Form1) returns False after the first call to Form1.Show even after Form1 is closed.

  6. #6
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    RobertX

    Upload a screen shot of the mdi parent before loading the child then one of when the mdi child is loaded, this will make it clearer to what your trying to do ?

    I guess with the buttons your trying to make some kind of toolbar, if so theres already a toolbar control which dosnt allow the mdi area to be infront of the control

    also to control opening child forms use this sub , the sub in the parent MDI

    VB Code:
    1. Public Sub ShowSingleInstance(ByVal childType As Type)
    2.         For Each child As Form In Me.MdiChildren
    3.             If child.GetType Is childType Then
    4.                 child.Activate() 'already loaded so bring to foreground
    5.                 Return
    6.             End If
    7.         Next
    8.         Dim frm As New Form
    9.         frm = Activator.CreateInstance(childType) 'not loaded yet so create
    10.         frm.MdiParent = Me
    11.         frm.Show()
    12.     End Sub

    use this to call the sub within the parent

    VB Code:
    1. ShowSingleInstance(GetType(FORM_NAME))

    If opening a mdi child from another child you need to do this

    add a module to your project Call it something like Settings

    define the parent from like this

    VB Code:
    1. Public MainApp as NAME_OF_THE_MD_PARENT
    also when you load the main parent add
    VB Code:
    1. MainApp = me

    then you can use
    VB Code:
    1. MainApp.ShowSingleInstance(GetType(FORM_NAME))

    that will allow you to open from another child and set the parent
    Last edited by carlblanchard; Mar 6th, 2004 at 03:25 AM.
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374
    I have used the MDI form to place buttons onto which forms my main menu. I will have to find some other way of doing this - either by hiding the buttons when a child is shown or by having the main menu on a separate child form.

    I just want to be able to show a form without creating multiple instances of the form and without having to show it modally.

  8. #8
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    So you are using buttons as a toolbar, ?
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374
    Yes. There would be too many items to fit across a tool bar unless each sub-item of the tool bar displayed a sub-menu on a another form.

  10. #10
    Fanatic Member carlblanchard's Avatar
    Join Date
    Sep 2003
    Location
    Bournemouth (UK)
    Posts
    539
    Ok in that case what i would do is use the sandbar control
    http://www.divil.co.uk/net/controls/sandbar/

    It allows for buttons, combo boxes, and best of all drop down menus, i think for what you are trying to do the drop down menus within a tool bar would work great, allowing you to orgranise all the buttons and fitting everything within the parent

    Hope it helps
    I am curretly building a defect management system for software and web developers,
    If you wana try it out (beta test) and keep it for free just send me a message

  11. #11
    Addicted Member Hole-In-One's Avatar
    Join Date
    Mar 2003
    Location
    Minnesota
    Posts
    195
    Not sure if this is what your looking for?

    Just dock a panel at the top of your form. Then stick the buttons on the panel.

    Kinda Crude:
    Attached Images Attached Images  

  12. #12

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Mdi Problem

    I have been using this code to activate MDI child forms:

    VB Code:
    1. Public Sub ShowSingleInstance(ByVal childType As Type)
    2.         For Each child As Form In Me.MdiChildren
    3.             If child.GetType Is childType Then
    4.                 child.Activate() 'already loaded so bring to foreground
    5.                 Return
    6.             End If
    7.         Next
    8.         Dim frm As New Form
    9.         frm = Activator.CreateInstance(childType) 'not loaded yet so create
    10.         frm.MdiParent = Me
    11.         frm.Show()
    12.     End Sub

    If the MDI child is either not loaded or is behind another MDI child, its Form_Activated event will fire. However, if the MDI child is already the uppermost form, the Form_Activated event does not fire even if I explicitly call frm.Activate at the end of the above code.

    How do I make the Form_Activated event fire regardless of the MDI child form's current state?

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Mdi Problem

    The forms Activated event is raised when the form becomes active. If your form is not becoming active because it is already active then there is no event. You could place a public method in the child form that does what you currently do in the Activated event handler. You would then call this method from the parent and call Me.Activate() within this method. That way the child form gets activated if it is not already active and your code gets executed even if the child was already active.

  14. #14

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Mdi Problem

    Thanks. I think that is the solution.

  15. #15

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Posts
    1,374

    Re: Mdi Problem

    How do I reference a specific child form from the MDI parent form? I can reference the active form with Me.ActiveMdiChild. ..... but that does not give me access to the form's methods as it does not reference the form specifically.

  16. #16
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Mdi Problem

    The ActiveMdiChild property is of type Form. You can use CType to cast it as the desired type, which will then give you access to all its members. If you are only going to have a single instance of each type of child form, assign each to a variable in the parent. That way you always have access to them. Each time you want to show a child, you check whether its corresponding variable is Nothing or IsDisposed and, if so, create a new instance, otherwise just activate the existing instance.
    VB Code:
    1. Private child As ChildForm
    2.  
    3. If child Is Nothing OrElse child.IsDisposed Then
    4.     child = New ChildForm
    5.     child.Show()
    6. End If
    7.  
    8. 'Your method.
    9. child.ActivateForm()
    Last edited by jmcilhinney; Jun 3rd, 2005 at 05:17 AM.

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