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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

YeavisAuthor Commented:
Oopss... I realize that the panel names don't match... please disregard that.
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?
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...
OWASP: Avoiding Hacker Tricks

Learn to build secure applications from the mindset of the hacker and avoid being exploited.

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!
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.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
YeavisAuthor Commented:
I like the user control idea... but is there a way I could maximize the control in the panel?
broadbentCommented:
Use mycontrol.Dock = DockStyle.Fill
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
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...
maralansCommented:
I know the feeling all too well.  Glad it works for you.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.