Click here to Skip to main content
15,908,455 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Improved question

To Answer 2 : Thanks, but I am not to show it only.

To Answer 1 :
(1) If I don't add the line of "this.Controls.Add(form_2)", the form_2 will be shown behind the form_1. So, I add this line in form_1 just below the line of "initialcomponet()", so that the form_2 can be shown in the form_1 client area.

Plus, I have to add "form_2.toplevel=false" before "this.Controls.add(form_2)", or, it will show message of "can not add top level control(form_2) to control(this)", and then it can not run in advanced.

(2) The highlight means to let top bar in form_2 has highlight blue color. Since I can only see gray blue color of top bar in form_2 even I click the form_2, or add any line of following in form_1 :
C#
form_2.Show();
form_2.Activate();
form_2.Enabled = true;
form_2.BringToFront();
form_2.Focus();


First question

I have a winform application with a form named form_1, I write code in form_1 to create form_2, and I would like to activate the form_2 with highlight color, however, it fails. The code is below :

1. I create form_2 and let it be included in form_1 (this)
C#
Form_help form_2 = new Form_help();
form_2.TopLevel = false;
this.Controls.Add(form_2);

2. I try to activate form_2 to highlight it with the following lines, but all fails to highlight it.
C#
form_2.Show();
form_2.Activate();
form_2.Enabled = true;
form_2.BringToFront();
form_2.Focus();

The question is how can I highlight the form_2? Since I would place multiple forms in form_1, and when click any of the forms, the clicked form will be highlight.

I also try the way of form_1.activecontrol(form_2) method, but it still fails.
Posted
Updated 29-Jul-10 21:55pm
v5
Comments
Sandeep Mewara 30-Jul-10 1:36am    
Use PRE tags to format code part. It makes the question readable.
Pawan Kiran 30-Jul-10 4:52am    
check f.Show(this) it may help you.

Are you trying to make form 1 contain multiple forms? If so, try using a mdi Parent Container instead. <-- Recommended over solution below.

Otherwise you can use P/Invoke:
http://www.pinvoke.net/default.aspx/user32.setforegroundwindow

It'll set any window to the foreground window or atleast try to, just use this.Handle to get the form's handle.
 
Share this answer
 
Comments
Sports Kuo 30-Jul-10 6:29am    
If the form_1 client area only have multiple forms, the mdiparent attribute works well, all forms can be seen, and the active one has highlight blue color.

However, my problem now is that there will be one 'toolstripcontainer1' control put inside the form_1 client area,so when I write code to set mdiparent attrubute be true for all child form, the forms can not be seen, if I add "toolstripcontainer1.contentpanel.controls.add(form_2)" for all child form, the forms then can be seen inside form_1, but the color will be gray color, how can I get the color be highlight ?
Mike$7 30-Jul-10 14:14pm    
Have you tried playing around with the mdi parent container in a new solution? Try making a new one, go to Project -> Add Windows Form -> MDI Parent Form. Then on the solution explorer, click Program.cs and replace: Application.Run(new Form1()); to: Application.Run(new YourMDIParentContainerNameHere()); Then debug!
C#
Form_help f = new Form_help();
f.ShowInTaskbar=false;
f.backcolor=color.red;
f.windowstate=formwindowstate.maximized;
f.show();
 
Share this answer
 
v3
Comments
Sports Kuo 30-Jul-10 6:03am    
Thanks, but it is not to show form only.
A form is not a control, you don't add it to the controls collection.

What do you mean by highlight ?
 
Share this answer
 
Comments
Sports Kuo 30-Jul-10 6:02am    
Thanks, please see my improved question.
Toli Cuturicu 30-Jul-10 6:06am    
Reason for my vote of 1
False. A Form IS a Control.

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