locked
Close one child and open a second child within the parent. RRS feed

  • Question

  • I have fought this 50 ways from Sunday yet I am not being successful.

    I am loading a parent named Menu1.

    I can open forms coded within Menu1 via the button bar at top of the parent with no issues.

    my MENU code in Menu1 (sample) is as follows:

        Public MenuForm As Menu1

    Private Sub MENU_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load TireMaint.MenuForm = Me Show() ChangeLoc() End Sub Public Sub Tires() If ActiveMdiChild IsNot Nothing Then ActiveMdiChild.Close() TireMaint.MdiParent = Me TireMaint.Show() End Sub

    Clicking on my 'Tire' Button on the PARENT form loads the TireMaint form perfectly.

    On my CHILD forms (TireMaint in this case) I want to Close TireMaint and load another form... let's say PMMaint form.

    I have the following on the TireMaint child form:

    Public MenuForm As Menu1

    Private Sub BtnPMMaint_Click(sender As Object, e As EventArgs) Handles btnPMMaint.Click Dim PreventMaint As New MinorMaint With { .MdiParent = MenuForm } If ActiveMdiChild IsNot Nothing Then ActiveMdiChild.Close() PreventMaint.Show() End Sub

    Issue: The PMMaint form opens, always outside the Parent Container then I end up with multiple windows needing closed. How do I close the TireMaint form then open the PMMaint form from the TireMaint form yet remaining inside the parent container?

     

    Sunday, July 26, 2020 10:56 PM

Answers

  • Hi

    Try this version. Needs Form1, Form2 and Form3, Form1 and Form2 have a Button1 on them.

    Form1 code

    ' Form1, Form2 and Form3
    ' Button1 on Form1 and Form2
    Option Strict On
    Option Explicit On
    Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	Button1.Visible = False
    	Dim nf2 As New Form2
    	nf2.TopLevel = False
    	nf2.Parent = Me
    	nf2.Show()
    End Sub
    End Class
    

    Form2 code

    Option Strict On
    Option Explicit On
    Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	Dim nf3 As New Form3
    	nf3.TopLevel = False
    	nf3.Parent = Form1
    	nf3.Show()
    	Close()
    	End Sub
    End Class

    Form3 code

    Option Strict On
    Option Explicit On
    Public Class Form3
    Private Sub Form3_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    	Form1.Button1.Visible = True
    End Sub
    


    Regards Les, Livingston, Scotland

    • Marked as answer by JohnK3JAE Wednesday, July 29, 2020 11:18 PM
    Monday, July 27, 2020 10:39 PM
  • Hi

    First of all, the code I posted works COMPLETELY, no ALMOST about it!

    That was not your original question.

    You asked about opening/closing Forms not about positioning them. You can position where you want, for example:

    With nf2
    	.Location = New Point(40, 100)
    	.TopLevel = False
    	.Parent = Me
    	.Show()
    End With
    


    Regards Les, Livingston, Scotland

    • Marked as answer by JohnK3JAE Wednesday, July 29, 2020 11:18 PM
    Tuesday, July 28, 2020 11:24 PM
  • Hi

    You have mage it a little easier to understand what you are trying to do.  From your description, I would approach this differently than the first attempt.

    Here is an example. This stand alone example needs Form1 with a horizontally split SplitContainer1 on it and 4 Buttons in SplitContainer1.Panel1.  Needs 3 further forms, Form2, Form3 and Form4.  Each of these Forms code is BLANK (with respect to the Form switching). This version has all of the switching Buttons at the top of Form1 and they remain visible throughout, and none of the child forms have switching capability - that is all handled from Form1 buttons.

    Try this out and see if it helps any.

    This is FORM1 CODE (other Forms have no Code)

    ' Form1, Form2, Form3 and Form4
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim Open As Form = Nothing
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Text = "THIS IS FORM1"
    		With SplitContainer1
    			.Dock = DockStyle.Fill
    			.SplitterDistance = 40
    			.FixedPanel = FixedPanel.Panel1
    			.Panel1.BackColor = Color.PaleGoldenrod
    		End With
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		If Not Open Is Nothing Then Open.Close()
    		Dim nf2 As New Form2
    		With nf2
    			.Text = "THIS IS FORM2"
    			.ControlBox = False
    			.Location = New Point(0, 0)
    			.Dock = DockStyle.Fill
    			.TopLevel = False
    			.Parent = SplitContainer1.Panel2
    			.Show()
    			.BringToFront()
    			Open = nf2
    		End With
    	End Sub
    	Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    		If Not Open Is Nothing Then Open.Close()
    		Dim nf3 As New Form3
    		With nf3
    			.Text = "THIS IS FORM3"
    			.ControlBox = False
    			.Location = New Point(0, 0)
    			.Dock = DockStyle.Fill
    			.TopLevel = False
    			.Parent = SplitContainer1.Panel2
    			.Show()
    			Open = nf3
    		End With
    	End Sub
    	Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    		If Not Open Is Nothing Then Open.Close()
    		Dim nf4 As New Form4
    		With nf4
    			.Text = "THIS IS FORM4"
    			.ControlBox = False
    			.Location = New Point(0, 0)
    			.Dock = DockStyle.Fill
    			.TopLevel = False
    			.Parent = SplitContainer1.Panel2
    			.Show()
    			Open = nf4
    		End With
    	End Sub
    	Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    		If Not Open Is Nothing Then Open.Close()
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, July 30, 2020 12:51 PM
    • Marked as answer by JohnK3JAE Friday, July 31, 2020 4:30 PM
    Thursday, July 30, 2020 12:51 PM

All replies

  • Hi

    Not sure what you are asking, but maybe this code can help.  It shows/hides a Form as a Child - in this case in a TabPage - TabPage(3) - or hides it using a Button on TabPage(0). Basically Button1 toggles Form2.

    There are many variations on doing this kind of thing and this is one of them.

    This is a stand alone example and needs Form1 with TabControl1 containing TabPages(0-3) and Button1 on TabPage(0). Needs Form2 with a ListBox1 on it.

    Form1 code

    ' Form1, Form2 (with ListBox1)
    ' TabControl with 4 Pages
    ' Button1 on TabPage(0)
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim nf As New Form2
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		' add new Form2 to TabPage(3)
    		With nf
    			.TopLevel = False
    			.Dock = DockStyle.Fill
    			.ControlBox = False
    			.lst.AddRange({"A", "B", "C", "D", "E"})
    			.ListBox1.DataSource = nf.lst
    		End With
    		TabControl1.TabPages(3).Controls.Add(nf)
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		' hide or show Form2 on TabPages(3)
    		Static b As Boolean = False
    		b = Not b
    		Select Case b
    			Case True
    				nf.Show()
    			Case Else
    				nf.Hide()
    		End Select
    	End Sub
    End Class

    Form2 code

    Public Class Form2
        Property lst As New List(Of String)
    End Class


    Regards Les, Livingston, Scotland

    Monday, July 27, 2020 12:05 AM
  • I am using a PARENT container. On that container I have buttons that opens a child form within the parent. Similar to your above related TAB code. These work fine.

    In simple terms, I want to open a new child form using a button on the currently active child form and then closing the current active child form once the new child loads leaving only the newly loaded child in the container.

    I do not want to hide the form as once the data has been entered and saved it is no longer needed and is not dependent or needed by any other child form that may be opened. I do not want to drop it to the 'background' where it is still visible in the parent, in some cases, if the new child is not taking up the entire parent container.

    Monday, July 27, 2020 9:29 PM
  • Hi

    Try this version. Needs Form1, Form2 and Form3, Form1 and Form2 have a Button1 on them.

    Form1 code

    ' Form1, Form2 and Form3
    ' Button1 on Form1 and Form2
    Option Strict On
    Option Explicit On
    Public Class Form1
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	Button1.Visible = False
    	Dim nf2 As New Form2
    	nf2.TopLevel = False
    	nf2.Parent = Me
    	nf2.Show()
    End Sub
    End Class
    

    Form2 code

    Option Strict On
    Option Explicit On
    Public Class Form2
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    	Dim nf3 As New Form3
    	nf3.TopLevel = False
    	nf3.Parent = Form1
    	nf3.Show()
    	Close()
    	End Sub
    End Class

    Form3 code

    Option Strict On
    Option Explicit On
    Public Class Form3
    Private Sub Form3_FormClosing(sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
    	Form1.Button1.Visible = True
    End Sub
    


    Regards Les, Livingston, Scotland

    • Marked as answer by JohnK3JAE Wednesday, July 29, 2020 11:18 PM
    Monday, July 27, 2020 10:39 PM
  • Here is the result of the code you submitted. ALMOST works but not quite.

    I'll try to explain:

    1. When using the code as given, the child loads at the extreme top of the container being hidden behind the menubar of the container.

    2. One of the pages load in the center as designed, yet is shifted severely to the right cutting off the right portion of the page.

    I have images of this but I'm obviously not allowed to post them or links until I am verified, whatever that even means. If there is a way to send you images I believe it would make things very clear as to what's happening.
    • Edited by JohnK3JAE Tuesday, July 28, 2020 11:20 PM simple edit
    Tuesday, July 28, 2020 11:11 PM
  • Hi

    First of all, the code I posted works COMPLETELY, no ALMOST about it!

    That was not your original question.

    You asked about opening/closing Forms not about positioning them. You can position where you want, for example:

    With nf2
    	.Location = New Point(40, 100)
    	.TopLevel = False
    	.Parent = Me
    	.Show()
    End With
    


    Regards Les, Livingston, Scotland

    • Marked as answer by JohnK3JAE Wednesday, July 29, 2020 11:18 PM
    Tuesday, July 28, 2020 11:24 PM

  • I have images of this but I'm obviously not allowed to post them or links until I am verified, whatever that even means. If there is a way to send you images I believe it would make things very clear as to what's happening.

    See 

    https://social.technet.microsoft.com/Forums/en-US/94f05325-8566-4c4c-806c-179a5a0beafc/verify-accounts-43?forum=reportabug


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, July 29, 2020 1:01 PM
  • I apologize if I offended you... That was not intended.

    I added in the 'with' statement and that is working now great.

    NEW issue. Once i run through the buttons and all the windows open as designed, if I use the menu in the container they do nothing now. Am I needing to re-work those now? Example of code in the container menu:

        Public Sub FuelData()
            If ActiveMdiChild IsNot Nothing Then ActiveMdiChild.Close()
    
            FuelInfo.MdiParent = Me
            FuelInfo.Show()
        End Sub
    Would I need or want to incorporate your suggested code as above?

    Wednesday, July 29, 2020 11:18 PM
  • Hi

    I am glad you are making progress, and thanks for the apology.

    I don't understand your further question. What do you mean by 'Container'? Especially, what is meant by 'the menu' in 'the container'? Please use the recognised control names such as ContextMenuStrip  or  MenuStrip  or  Child Form  etc.

    Also, when necessary,  please include comments on what a control is being referenced in your code - for example is  'FuelInfo' one of the Child Forms or a regular form or a Dialog?

    When you say 'if I use the menu in the container they do nothing now' what does that mean exactly?



    Regards Les, Livingston, Scotland

    Wednesday, July 29, 2020 11:31 PM
  • I had hoped my account would have been validated by now to make this easier by being able to post screenshots but alas, still waiting.

    My MDI PARENT form has navigation menu buttons along the top of the parent. It is otherwise blank. When I choose any button to load any child form from the MDI parent navigation bar it then loads the called child into the 'blank' area of the parent below the parent navigation bar.

    With the added code to the child form buttons, as you previously helped with and I use those newly coded buttons on any child form, the parent form navigation buttons at the top appear now not to be working aka no child form loads.

    Example:

    1. When the program loads, it loads the MDI parent form 'Menu1' and by defaults loads the child form LoadInfo below the MDI parent menu strip at the top.

    2. Using the menu strip of the MDI parent I then choose the button 'MaintInfo' child form (as an example) and the LoadInfo child is closed and the MaintInfo child loads.

    3. I then can use any button on the MaintInfo child to load any child chosen - in my case TireInfo child which loads fine and closes the MaintInfo child, with the code you suggested.

    4. If I then try and use the MDI PARENT navigation buttons at the top now nothing appears to load, as if the Parent Nav buttons are no longer working.

    I hope that helps clear that up... I'm not well versed on all the 'proper' terminology so I apologize for my ignorance.

    Thursday, July 30, 2020 7:53 AM
  • You know I was thinking and had an idea, although not 100% sure how to implement.

    Rather than the approach I was working I was wondering if passing the click event of the child button to the parent control (the nav bar at top of the parent) through a raised event was possible?

    Although this is a new thought and I understand this could possibly be done I am completely unaware how to fully implement that. Would this be easier than what I am currently working on and can you give a little guidance on how to approach this idea?

    Thursday, July 30, 2020 8:58 AM
  • Hi

    You have mage it a little easier to understand what you are trying to do.  From your description, I would approach this differently than the first attempt.

    Here is an example. This stand alone example needs Form1 with a horizontally split SplitContainer1 on it and 4 Buttons in SplitContainer1.Panel1.  Needs 3 further forms, Form2, Form3 and Form4.  Each of these Forms code is BLANK (with respect to the Form switching). This version has all of the switching Buttons at the top of Form1 and they remain visible throughout, and none of the child forms have switching capability - that is all handled from Form1 buttons.

    Try this out and see if it helps any.

    This is FORM1 CODE (other Forms have no Code)

    ' Form1, Form2, Form3 and Form4
    Option Strict On
    Option Explicit On
    Public Class Form1
    	Dim Open As Form = Nothing
    	Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Text = "THIS IS FORM1"
    		With SplitContainer1
    			.Dock = DockStyle.Fill
    			.SplitterDistance = 40
    			.FixedPanel = FixedPanel.Panel1
    			.Panel1.BackColor = Color.PaleGoldenrod
    		End With
    	End Sub
    	Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    		If Not Open Is Nothing Then Open.Close()
    		Dim nf2 As New Form2
    		With nf2
    			.Text = "THIS IS FORM2"
    			.ControlBox = False
    			.Location = New Point(0, 0)
    			.Dock = DockStyle.Fill
    			.TopLevel = False
    			.Parent = SplitContainer1.Panel2
    			.Show()
    			.BringToFront()
    			Open = nf2
    		End With
    	End Sub
    	Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    		If Not Open Is Nothing Then Open.Close()
    		Dim nf3 As New Form3
    		With nf3
    			.Text = "THIS IS FORM3"
    			.ControlBox = False
    			.Location = New Point(0, 0)
    			.Dock = DockStyle.Fill
    			.TopLevel = False
    			.Parent = SplitContainer1.Panel2
    			.Show()
    			Open = nf3
    		End With
    	End Sub
    	Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
    		If Not Open Is Nothing Then Open.Close()
    		Dim nf4 As New Form4
    		With nf4
    			.Text = "THIS IS FORM4"
    			.ControlBox = False
    			.Location = New Point(0, 0)
    			.Dock = DockStyle.Fill
    			.TopLevel = False
    			.Parent = SplitContainer1.Panel2
    			.Show()
    			Open = nf4
    		End With
    	End Sub
    	Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
    		If Not Open Is Nothing Then Open.Close()
    	End Sub
    End Class


    Regards Les, Livingston, Scotland


    • Edited by leshay Thursday, July 30, 2020 12:51 PM
    • Marked as answer by JohnK3JAE Friday, July 31, 2020 4:30 PM
    Thursday, July 30, 2020 12:51 PM
  • Hi

    As a pont of interest - why do you want to use child Forms to do this instead of just using available Form controls such as Panels etc to achieve your needs?


    Regards Les, Livingston, Scotland

    Thursday, July 30, 2020 1:34 PM
  • The above example is very close what I am currently doing. Although I had not considered using a split container on the Parent. That is a brilliant' suggestion.

    The idea of the buttons on the child forms was just to offer an alternate way of swapping out the child forms without the need to necessarily or only use the Form1 buttons (which is Menu1 in my project but using your above naming schema example as a means to clarify).

    I did successfully change the button code on the child forms to pass the click event to the parent control... my issue was I had all the 'Click' events on the parent as PRIVATE instead of PUBLIC. I think passing the click event to the parent resolves my immediate issue at hand although I have not thoroughly tested it yet. It looks to be working via debug.

    I have copied the above proposed code, which is much more readable/understandable than my chicken scratch code and I will implement this over the next few days.

    I do wish to thank you sincerely for taking the time to understand my initial issue and assist in offering some very solid and good advice. I am an amateur coder, at best, and learn new things often by trial and error.

    Friday, July 31, 2020 4:53 PM
  • See response above. :)
    Friday, July 31, 2020 4:54 PM