Click here to Skip to main content
15,900,589 members
Please Sign up or sign in to vote.
2.72/5 (4 votes)
See more:
I'm kinda new into C# and would like to know how I can place a Form inside a Panel.
Google helped understand a bit the concept of it, but I can't seem to be able to pull it off yet.

If anyone could help me, i'll be very thankful.

Thank you !
Posted
Updated 8-May-23 11:09am
Comments
AshishChaudha 2-Jul-12 6:50am    
web or windows??

Just to add to Ganesan Senthilvel's solution you should set the following also to remove the windows border and max/min buttons etc.
C#
myForm.FormBorderStyle = FormBorderStyle.None;
 
Share this answer
 
Comments
Steven Borges 2-Jul-12 7:11am    
Yeah, I was going to make this much already, but will 5'ed you anyway, because people that are even more as a newcomer than me will find this useful aswell ;)
You would be better off creating every panel as a UserControl. These are just like forms, but without the window elements. Here is the sample code:

C#
Form1 myForm = new Form1();
myForm.TopLevel = false;
myForm.AutoScroll = true;
frmMain.Panel2.Controls.Add(myForm);
myForm.Show();
 
Share this answer
 
Comments
Steven Borges 2-Jul-12 6:52am    
Thank you, this worked, altough I didn't use the frmMain in your code, I just used:
Panel2.Controls.Add(myForm) and it seems to work perfectly.
loserkidz32 21-Nov-13 5:21am    
in my panel have a text box and form but when i running this userControl , the userControl place override with my form (textbox and label) how to fix that?
Mehdi Gholam 2-Jul-12 6:53am    
5'ed, and added a little more in my solution :)
elaznajib 2-Nov-14 20:36pm    
i use this solution the last weekend it's work but if you use controllname.Select(); just use this.controls.clear(); in the onclose() function to reduce the space in RAM
Nganku Junior 6-Dec-16 18:38pm    
Cudos bro!!! Great code
It doesn't seem to work perfectly. If you use it to click the button
public void Xe_ExamSchedulerMethod()
   {
    // Create a new instance of the form you want to show inside the Panel control
    Form1() frm = new Form1();   

    // Set the TopLevel property of the form instance to false
    frm.TopLevel = false;

    // Set the Dock property of the form instance to Fill
    frm.Dock = DockStyle.Fill;

    // Add the form instance to the Controls collection of the Panel control
    panel1.Controls.Add(frm);

    // Show the form inside the Panel control
    frm.Show();
   }
 
Share this answer
 
v5
Comments
Dave Kreskowiak 8-May-23 18:27pm    
11 years too late to the discussion and you haven't added anything to conversation that hasn't already been posted in two other answers.
Form1 myForm = new Form1();
myForm.TopLevel = false;
myForm.AutoScroll = true;
frmMain.Panel2.Controls.Add(myForm);
myForm.FormBorderStyle = FormBorderStyle.None;
myForm.Show();
 
Share this answer
 
Comments
BillWoodruff 3-Dec-14 3:19am    
You have answered a question more than two years old, and added nothing that was not already in other answers.
Member 13927827 15-Aug-18 4:01am    
the frmMain.Panel2 was with error, do u all facing the same?
stefan27dk 9-May-20 12:34pm    
Hi you just write the name of your panel in wich you want to load the form.
Like panel2.Controls.Add(form3);

If you want to load different forms in one general panel wich is let say in the middle and you have buttons over the panel, so when you press the a button different form will appear in the "panel2" I call it Loader_panel.


private void HideAllForms()
{
SagerForm2.Hide();
AdvokaterForm3.Hide();
MedarbejderForm6.Hide();
SekretaerForm7.Hide();
YdelserForm8.Hide();
KunderForm9.Hide();
Form10.Hide();
HomeForm11.Hide();
DeveloperForm12.Hide();

}


//"Hide all other Forms that are meant to be displayed int the panel"
HideAllForms(); // This is a Method

Form10.TopLevel = false; // If its not false you cant add its controls to the panel
Loader_panel.Controls.Add(Form10); // "Loader_panel" is the name of the panel in wich you will load the forms
Form10.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None; //Removes the border
KoerselForm10.Dock = DockStyle.Fill; // This is important so the form autoresizes on Window resize


otherwise it is going to stay the same size even if you resize the window. Ofcourse you will need to have the Loader_panels Dock Style to be let say middle. So you have panel with buttons on the top Wich is dockStyle Top and Loader_panel dockStyle middle. So When you resize your form the loaded forms in the Loader_panel will rezise as the window.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900