Help. Can't display a form in a Panel/window.

Mugsy's RapSheet 176 Reputation points
2023-08-10T13:20:27.5233333+00:00

I'm trying to update my app by moving the primary interface into a scrollable window (because the form was getting too big to fit lower rez screens.)

I renamed my frmMain to "frmMainChild" (from the Solution Explorer) and created a new Startup form with a big "Panel" object on it. Then in the "Form_Load" sub, I (try to) load the (renamed) form into the panel:

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        frmMainChild.TopLevel = False       ' Suppress error.
        pnlMain.Controls.Add(frmMainChild)  ' Load frmMainChild into Panel.
        Me.Show()
    End Sub

Doesn't work. Panel remains empty. Not sure what's missing.

TIA

VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,824 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Anonymous
    2023-08-11T12:08:23.7366667+00:00

    Hi

    This may help.

    I have a small working app. It happens to only have the default Form1.

    To do what I think you want, I added a new Form and named it MyMainForm, of course, you would choose your own name and substitute accordingly.

    Next, double click on My Project in Solution Explorer to open configuration window.

    There, I set the Application -> Start Up Form to the new Form (MyMainForm)

    I added a Panel1 in the Designer to MyNewForm and set Dock to Full.

    Next, I added this code to MyNewForm:

    Public Class MyMainForm
    	Private Sub MyMainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    		Dim f As New Form1
    		With f
    			.TopLevel = False
    			.Parent = Panel1
    			.Dock = DockStyle.Fill
    			.FormBorderStyle = FormBorderStyle.None
    			.AutoScroll = True
    			.Show()
    		End With
    	End Sub
    End Class
    

    The various Properties can be adjusted to suit, but I would suggest trying to run each time you edit/remove/add Properties so that any problems can be pinned down to the last edit.

    NOTE: I did not change anything in the original Project code at all.

    If you want to try this out (best to try first). then use any existing working Project you already have, make a backup just in case (all that is needed is Copy Project folder, and Paste to same parent folder - file system will add a '- Cop(n)' to name)

    Using the existing Project that you have just backed up, follow the above and see what happens.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.