Docking one form to another

New to VB.Net programming. I want to open Form2 from a button on Form1. Got this far. 
Form2 has no border, I want it to appear to be part of Form1, (like a frame in a web page, everything else on the page stays the same except for this Form2 overlay). I need to position Form2 before showing (I think I can do that part) but then if user moves form1 or resizes it, Form2 is now left hanging in the wrong place.

Another way to look at this is the idea of Access forms and subforms. Here we embedded one form into another, and the subform always stayed attached. 

What is the best way to do this in VB  Any help and guidance is appreciated...


Answer this question

Docking one form to another

  • SQL McOLAP

    Try adding the following line:

        frm.BringToFront()

    and that should take care of it.  If not, let me know and I'll check it out.

  • gatalec

    Thank you for that code.

    Now one further question. When I "close" Form2 in this manner, I want Form2 to check for changed records, etc. and warn the user to update data before closing. I tried to put a msgbox message in the form2.closing eventhandler but it appears to not be called. Is this because it is now a control

    How would I do this

  • Mathijs van Bockhooven

    I'm not sure if this will help you, but a while back i posted a <a href="http://www.gotdotnet.com/Community/UserSamples/Details.aspx SampleGuid=80e3a4fc-2713-4939-9c2d-ed0d1b9d3a3f">Specialed Forms Collection</a> that takes care of what you're trying to do pretty easily.

    <a href="http://www.erikporter.com/images/s1.png">We</a> <a href="http://www.erikporter.com/images/s2.png">actually</a> <a href="http://www.erikporter.com/images/s3.png">used</a> <a href="http://www.erikporter.com/images/s4.png">it</a> <a href="http://www.erikporter.com/images/s5.png">for</a> <a href="http://www.erikporter.com/images/s6.png">this</a>

    the center of each one of those screens is a form setup like i think you're looking for.  Basically the Forms Collection above will help you do what Ken and Jacob have been describing.

  • wrvjohn

    An alternative solution may be to add the Form to the controls collection of the container.  Then it should move right along with it automatically.  To add a form to another form, you need to do something like this:

      Private frm As Form2

      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)  Handles Button1.Click
        frm = New Form2
        frm.Location = Panel1.Location
        frm.TopLevel = False
        frm.Size = Panel1.Size
        Me.Controls.Add(frm)    
        frm.Show()
      End Sub


    That should do the trick.  If not, it should only require minimal tweaking.

  • P3T3R 7

    Well, if this is a situation where you will only, at any give time, have one instance of this form in your TopLevelForm's control collection, then you could do something like this:

    Dim i As Integer = 0
    Dim blnFound As Boolean = False

    Do Until blnFound Or i = Me.Controls.Count
         If TypeOf Me.Controls(i) Is Form2 Then
              blnFound = True
         Else
              i += 1
         End If
    Loop

    If blnFound Then
          Me.Controls.RemoveAt(i)
    End If

  • Silk Brick

    There may be other (less ugly) alternatives, but here's one I just mocked up. Given Form1 with a Panel control named Panel1 on it (the panel indicates where you want the "subform" to go) and a button named Button1, you could use code like this:

      Private frm As Form2

      Private Sub Button1_Click( _
       ByVal sender As System.Object, _
       ByVal e As System.EventArgs) _
       Handles Button1.Click
        frm = New Form2
        frm.Location = PointToScreen(Panel1.Location)
        frm.Size = Panel1.Size
        frm.Owner = Me
        frm.Show()
      End Sub

      Private Sub Form1_Move( _
       ByVal sender As Object, _
       ByVal e As System.EventArgs) Handles MyBase.Move
        If Not frm Is Nothing Then
          frm.Location = PointToScreen(Panel1.Location)
        End If
      End Sub

    This just opens the new form when you click the button, and moving the main form causes the child form to move with it. Again, this is the "brute force" solution, and I'd love to see one that works better. 

  • stalamoni

    Thank you, I will give this a try.
  • ger001

    When I tried your solution, I got no errors, but no form appeared on my screen anywhere. Any ideas
  • Jazz1234

    Using my solution, the primary title bar should not go inactive.
  • Mhorn821

    Jacob,

    This worked great!  I also added the frm.dock = dockstyle.Fill to keep the size correct.

    Now another question:

    How do I reference this "control" so as to "close" it when I want to open a different form instead  I have tried to figure out the control collection and while I have figured out that the name of this form control is the same name as the form class from which it is created, I can't seem to get the main form (form 1) to identify and remove it. I am sure it's something simple, but help me.....


  • jadoger

    Hurray! It works. Now for one final thing: Is it possible for the main form to not flash inactive (Title bar changes color) when the user clicks anywhere in the second form. Since they are essentially two different windows, one is active the other not. Is there any way to truly embed one form into another
  • renegade800X

    I tried your code, as well as the other posted reply, and neither worked. Your code displayed the form, but not in the right place. It did not open on top of the panel from form 1. I have another button that closes Form2. Each time I closed form2 and reopened, it opened in a different place on the screen.

    The other code option did not show the form at all. Nothing appeared, even though I got no errors.

    Any more help guys

  • Ranjith_Msdn

    We neglected to mention that you have to set the form's StartPosition property to Manual (that's not the default). WIthout this, you can't control where it appears on the screen. Oops.
  • Muhammad Ismael

    hrm... Your scenario works for me...  Can you post the Closing event handler 
  • Docking one form to another