URGENT - Form Inside Panel, Problems resizing content correctly

Experts,
    Rather than using MDI I've opted to load forms inside of a panel for presentation purposes. My program is nearly complete, however I'm having issues when my form gets resized. My main form "frmPropertyTracker" has a panel on it called "pnlMainContent" and then within that panel I am displaying the new forms based upon the users selection. I used the following code that I found on EE and it has worked great except when I resize frmPropertyTracker the form displayed inside the panel stays maximized and does not resize the content accordingly. Ideally I would like it resize based on the size of the panel... if anyone can help I would apprecaite it!!

Private Declare Function SetParent Lib "user32" Alias "SetParent" (ByVal hWndChild As Integer, ByVal hWndNewParent As Integer) As Integer

    Private Sub btnLeaseSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLeaseSearch.Click
           Dim frmPropertyTrackerLeases As New frmPropertyTrackerLeases
           SetParent(frmPropertyTrackerLeases.Handle.ToInt32, pnlFormContainer.Handle.ToInt32)
           frmPropertyTrackerLeases.WindowState = FormWindowState.Maximized
           frmPropertyTrackerLeases.Show()    
    End Sub

The other issue I'm having with this code is that I would like to know how to close down other open forms before I run the code above so that I don't have 500 forms floating around in memory. Thanks in advance to anyone that can help...

Jason
YeavisAsked:
Who is Participating?
 
maralansCommented:
Another alternative would be to use User Controls rather than regular forms.

Private Sub lblDelInst_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lblDelInst.Click
        Dim tempObject As New ucPropertyTrackerLeases      ' Create an object of the user control.
        pnlFormContainer.Controls.Clear()                              ' Clear anything that the panel is displaying.
        pnlFormContainer.Controls.Add(tempObject)              ' Add the control to the panel which will automatically display it.
0
 
YeavisAuthor Commented:
Oopss... I realize that the panel names don't match... please disregard that.
0
 
broadbentCommented:
I understand the problem as follows.
You have a panel - pnlMainContent and you wish to display various forms within it, presumably when some button or other is pressed.
I assume these forms don't have borders.
Effectively you have tabpages, so why not use a tab control?
0
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
YeavisAuthor Commented:
Say I implemented the tab control, how much customization could I do as far as appearance? What I'm looking for is something that fits right into the form without adding bulking edges or visual styles. The method I have in place now works beautifully but as I mentioned I have this issue. If you could code up and example of a simliar situation (where it all fits inside a panel) then I would be happy to consider using the tab control. I'm new to vb.net and I haven't played with the tab control in previous versions of vb either...
0
 
broadbentCommented:
I don't like the tabcontrol only because it doesn't let me use a transparent background, but writing your own is quite difficult.
That apart, iot is a very standard implementation for applications, and you should at least try it.
Open a completely new project and on the blank form add the tabcontrol. Play with it!
0
 
maralansCommented:
Open a new project.  Put 2 buttons and a panel on form1.  Add 2 user controls making the backgroud colors different colors and paste the following code to try.

Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim tempObject As New UserControl1    ' Create an object of the user control.
        Panel1.Controls.Clear()                              ' Clear anything that the panel is displaying.
        Panel1.Controls.Add(tempObject)              ' Add the control to the panel which will automatically display it.
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim tempObject As New UserControl2     ' Create an object of the user control.
        Panel1.Controls.Clear()                              ' Clear anything that the panel is displaying.
        Panel1.Controls.Add(tempObject)              ' Add the control to the panel which will automatically display it.
    End Sub
End Class
0
 
YeavisAuthor Commented:
I like the user control idea... but is there a way I could maximize the control in the panel?
0
 
broadbentCommented:
Use mycontrol.Dock = DockStyle.Fill
0
 
maralansCommented:
If your playing with the above example, a simple way is to make your user control the same size as the panel. Then if you want to keep the user control contained in the panel use:

Panel1.Dock = DockStyle.Right
0
 
YeavisAuthor Commented:
Awesome! I have been beating my head on this for a couple days and now it works like a charm. I appreciate all your help...
0
 
maralansCommented:
I know the feeling all too well.  Glad it works for you.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.