Click here to Skip to main content
15,892,575 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I want to open a form2 in form 1. I am using panel there are two panel in form 1. Panel1 contains button and in pannel 2 i want to show the windows form on click of button which is in panel1.


here is my code
private void metroButton1_Click(object sender, EventArgs e)
        {
            using (CustomerReg CustReg = new CustomerReg())
            {
                pnlCustReg.Controls.Clear();
                nav(CustReg, pnlCustReg);
            }
        }


public void nav(Form form, Panel panel)
        {
            form.TopLevel = false;
            form.Size = panel.Size; // for responsive size
            panel.Controls.Clear();
            panel.Controls.Add(form);
            form.Show();
        }


What I have tried:

i am not able to show form 2 in form 1 panel2. and i tried all methods to display data plz help.
Posted
Updated 15-Sep-19 23:03pm

You normally would not do this. Instead of using a Form, you would make your "form" as a UserControl and show the control in the panel instead.

The problem with showing the Form "as a control" in a Panel is that the user will still be able to manipulate the form, move it around in the Panel, minimize it, maximize it, even close it!, just like the same form on the Desktop.

But, to answer your question, you have to call a Win32 function, SetParent, with the WindowHandle of your Form and the WindowHandle of your Panel control to make the Panel the new parent of the Form. Examples:

Tabbed Multi Process Application[^]
Existing EXE run with in a window form using c#(WPF)[^]
 
Share this answer
 
Comments
BillWoodruff 13-Jun-19 9:56am    
+5 I would add to Dave's wise words: don't put a Form in another Form: it's an "expensive" object, and it's not meant to be used that way. You can use Panels defined in your app as well as UserControls.
OriginalGriff 13-Jun-19 10:28am    
+5 from me as well. "Just because you *can* do something, ..." :laugh:
Maciej Los 14-Jun-19 13:21pm    
I must admit that i was forced to vote a 5!
:)
AntGamble 16-Sep-19 5:05am    
You don't need to call Win32 functions - use use the 'Parent' property - this even works on Linux via mono.
The answer is really simple....

public void nav(Form form, Panel panel)
        {
            form.TopLevel = false;
            form.FormBorderStyle = FormBorderStyle.None;
            form.Parent = panel;
            form.Dock = DockStyle.Fill;
            form.Show();
        }


....and to remove it, just set the form's parent to null - if you want to show it by itself, you'll have to reset the borderstyle/toplevel/etc. this code assumes it'll always be in a container.

public void navclose(Form form)
        {
            form.Parent = null;
        }
 
Share this answer
 

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