Loading and showing a form in a Panel control

Is it possible to load and display a form inside a panel control using Visual Basic 2005 and if so what code do I need to handle this.

Regards

Dipendra



Answer this question

Loading and showing a form in a Panel control

  • jplh

    Actually, I tried it and it does work nicely - the key element when I tried it previously (and missed) was the .TopLevel property....

    What you could do, is set the borderstyle (of the form) to None and see how that goes. Even so, logically, UserControls would probably be better, but it's good to know that the above works.



  • Zartor

    There is an example on my site entitled, "Add forms as user controls", which allows you to use forms as controls and have their Titlebars change focus.

    http://www.dotnetrix.co.uk/misc.html


  • zdjray


    This is the code that I use. I'm sure I picked it up from someone else, so I won't take any credit for it. This code assumes that you have a split container (SplitContainer1) on the form that contains two panels (Panel1 and Panel2). You can could add the controls (buttons or link labels, etc.) that call the ShowView function to Panel1 and the forms will be loaded as controls to Panel2.

    Private Sub ShowView(ByVal childView As Control)

    Cursor = Cursors.WaitCursor

    If _currentView IsNot Nothing Then

    If TypeOf _currentView Is Form Then

    Dim myform As Form = _currentView

    myform.Close()

    Else

    _currentView.Visible = False

    _currentView.Dispose()

    End If

    _currentView = Nothing

    End If

    childView.Hide()

    If TypeOf childView Is Form Then

    Dim childForm As Form = childView

    childForm.TopLevel = False

    childForm.FormBorderStyle = Windows.Forms.FormBorderStyle.None

    End If

    Me.SplitContainer1.Panel2.Controls.Add(childView)

    childView.BringToFront()

    childView.BackColor = Me.BackColor

    childView.Dock = DockStyle.Fill

    childView.Show()

    _currentView = childView

    Cursor = Cursors.Default

    End Sub

    I then call the function like this:

    Dim childView As Form = New Suppliers
    ShowView(childView)


  • Jose M. Ladero

    I wouldn't normally critisize someone trying to help, but don't put such code in the paint event: at best it's a hack - it's a disaster repeating itself (Question: how many times can a paint event fire in a second for a form which may never move ) . There are much better ways of getting the end result. The resize event would be better, but it's still a hack because we are forcing a form to appear as if it's contained.

    I can't see a reason not to use UserControls over a Form (there are a couple of things I can think of, but they really are not compelling enough).



  • Jon_The_Coder

    Thanks Whiteley

    Ans: The Paint event will fire once. But if we are putting breakpoint in the event handler, then it will be endless. Normally t will be fired only once.

    I didn't choose resize because of "If user moves the location of control, not resizing", then it will not be handled.

    Anyhow, Thanks to rjschave

    you gave the proper guide.

    I think your code is very propossional one.

    I checked, It is working fine.



  • tonic999

    Hi

    I am not sure.

    But this may help you

    Private Sub Panel1_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles Panel1.Paint

    My.Forms.Form2.Show()

    My.Forms.Form2.Left = e.ClipRectangle.Left + Me.Left + 4

    My.Forms.Form2.Top = e.ClipRectangle.Top + Me.Top + 29

    My.Forms.Form2.Width = e.ClipRectangle.Width

    My.Forms.Form2.Height = e.ClipRectangle.Height

    My.Forms.Form2.BringToFront()

    End Sub

    Just I put this coding in Panel.Paint event,

    I resized the form border into panel border.

    that 4 is parent for border width,

    and 29 is form header height.

    if it is not working, create dummy app, create two forms as Form1 and Form2

    Create panel in Form1 and put this coding in Panel1.paint event.

    and run it.



  • Apurva Sinha - MSFT

    I have a main home form with all the buttons at the top. I am so far using usercontrols to load into a panel on the home form as I thought that this was the only way around the issue therefore the home form is always visible. I am not going to use MDI forms as it is quite old fashioned.

    Regards


  • Kramish

    I have already started to use Usercontrols as they are better. I simply just checked whether it was possible to load forms into panels but thanks for all the responses.

    SJ Whiteley

    Since you had recommended usercontrols from a previous question, I am now using this in my IT asset tracking application and the Movie Starter Kit template provided by Microsoft also uses usercontrols and I am also using this to assist me.

    Regards


  • ploc

    I tried this code but unfortunately you can still move the 2nd form inside the panel control within form1 therefore usercontrols is the way forward.
  • boraaki

    Is there a problem with using user controls This isn't really a 'workaround' for anything, since forms are not normally hosted within other forms (MDI excepted).

    Howevery, you can set a forms parent using the SetParent() Win32 API (I believe), but is pretty much redundant as using usercontrols is probably the best way to do what you want.



  • Charles Aimer

    Venkat2Day, a form paint event can fire quite a few times a second - possibly more than 10 or even 100 times (unlikely but possible): with nothing happening to the form itself. Each time the form is exposed, a paint event will fire (for example moving another application across the form). If you are doing some heavy duty form manipulation in that form paint event, it may even cause a cascading paint event on the other form, and is quite possible to get stuck in a race condition

    I say all this for the benefit of others reading this post: the paint event should only be used to 'paint' the form - keep it as short and sweet as possible - as it is an event which can fire quite rapidly.

    Here's an (albeit contrived) example:

    In a new application, paste the following two lines into the paint event of a form and look at the Immidiate window, and the CPU usage.

    Debug.WriteLine("I got Painted!" & Environment.TickCount.ToString)
    Me.Refresh()



  • cookieCutter

    Hi. I don't think it will be possible. I tried to make a panel be the parent of a form and I received a huge exception. It doesn't sound logical to me to do that but as far as I tried it didn't work. What are you trying to do (if we can know of course) Are you trying to do a MDI app something like Outlook Express or Ms Word or something like that

    regards.


  • Ted Glaza

    VBArch

    I have tried the above and this actually does work. Therefore I can either load usercontrols or forms inside a panel within another form.

    SJWhiteley

    You may want to try this code as it seems to be faster than usercontrols but I guess it depends on preference really.

    Regards


  • Ikuko O.

    'Create a New project throw a panel on it and add this code, call your other form. I can not take credit for this Billy Hollis posted it on the Adventures in Visual Basis msdn site, but this works like a charm.

    Private Sub LoadForm(ByVal oForm As Form, ByVal Title As String)

    'Change the Caption

    Text = System.Windows.Forms.Application.ProductName + " " + Title

    'Set the form up for display

    With oForm

    .TopLevel = False

    .ShowInTaskbar = False

    .Size = Panel1.Size

    '.AutoScale = True

    Panel1.Controls.Add(oForm)

    .Show()

    End With

    End Sub


  • Loading and showing a form in a Panel control