Monday, June 4, 2012

How to embed a form in a control – จับ form มาใส่ 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…
01public void ShowFormInControl(Control myctl, Form myfrm)
02{
03 
04myfrm.TopLevel = false;
05myfrm.FormBorderStyle = FormBorderStyle.None;
06myfrm.Dock = DockStyle.Fill;
07myfrm.Visible = true;
08myctl.Controls.Add(myfrm);
09 
10 }
11 
12 private void button1_Click(object sender, EventArgs e)
13 {
14anotherForm myform = new anotherForm();
15ShowFormInControl(panel1, myform);
16 
17moreForm myform2 = new moreForm();
18ShowFormInControl(groupBox1, myform2);
19 
20 }
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.

you can download code example here frmAdvance.zip

ref : http://gpluspluss.com/2010/07/24/how-to-embed-a-form-in-a-control-%E0%B8%88%E0%B8%B1%E0%B8%9A-from-%E0%B8%A1%E0%B8%B2%E0%B9%83%E0%B8%AA%E0%B9%88-form/

No comments:

Post a Comment