Home > C# .NET, Windows Forms > How to embed a form in a control – จับ from มาใส่ form

How to embed a form in a control – จับ from มาใส่ form

it’s another time there is some asking about how to show form into another form. i have been trying to find around the web to get the example . but i found the answer myself as following example. . . [try to understand my bad English] . ha ha

in this posting i will show how to put existing  two form into one main form , by the way i’m doing this in main form i put the tab with two tab pages , first tabpage for first form and anther tab put second form. as following picture.

figure 1. show two tab in main form

By doing this i  create the project   with main form name form1.cs and another two is anotherform.cs and moreform.cs

First – put two tab into mainform (form1.cs)  and place panel1 into tabpage1 and groupbox1 into tabpage2 [please note this the control must be a container control]

then – Create another two form , anotherform.cs and moreform.cs

anotherform.cs

figure 2. show anotherform.cs

in this form show information of two textbox when you click  ‘show information’  button

moreform.cs

figure 3. show moreform.cs

in this form the textbox will show the selected text of  above combobox…

I will not go in detail of how these two form work i will explain how it work  in the main form.

in main form (form1.cs)  , see the following code, better then  explain…

public void ShowFormInControl(Control myctl, Form myfrm)
{

myfrm.TopLevel = false;
myfrm.FormBorderStyle = FormBorderStyle.None;
myfrm.Dock = DockStyle.Fill;
myfrm.Visible = true;
myctl.Controls.Add(myfrm);

 }

 private void button1_Click(object sender, EventArgs e)
 {
anotherForm myform = new anotherForm();
ShowFormInControl(panel1, myform);

moreForm myform2 = new moreForm();
ShowFormInControl(groupBox1, myform2);

 }

in the source code you can see  the technique of how to put the form into control  in the  “ShowformInControl(Control myctl, Form myfrm)  ”    the function perform putting the form ( myfrm ) into Control( myctl ) as i mention earlier – the control must be container control.  without any explanation it’s obviously show in the code…

then the forms are load by the click event of button1.    – create form and put into control by calling ShowformInControl where ever you want to put , panel or groupbox or any contrainer control.

hope this will help …   s_teerapong2000@yahoo.com

you can download code example here frmAdvance.zip

Categories: C# .NET, Windows Forms
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: