Click here to Skip to main content
15,913,939 members
Please Sign up or sign in to vote.
4.67/5 (2 votes)
See more:
Hi, probably a very simple question, but I don't seem to find the solution my self.

I've added a panel in my c# project.

How can I switch in the designer between the main form layout and the panel(s)?
Also (but probably within the same answer as the above question) how can I add a textbox to a pecific panel or main form?

Regards.

UPDATE:
After some experimenting with this I came to this "workable" solution:
If you want to work with multiple panels which are being activated by a menu option here's a way which worked for me:

In my test case I have
1 Form: Form1
1 Menustrip: menuStrip1
1 MenuStrip option1: ToolStripMenuItem1 (activates Panel1)
1 MenuStrip option2: ToolStripMenuItem2 (activates Panel2)
1 Panel: Panel1
1 Panel: Panel2

I want the panels to fill the whole form when clicked from the menustrip option.

The problem was that I didn't understand that there's a simple way of selecting a panel in the GUI without moving them around.
Still this is not clear to me.
There's always one which is directly visible in VS.

So if you add the 2 panels to the main form, and give them Dock: Fill, you can't reach the main Form or the first added panel.
As the last added panel is visible.
You could think that you add all needed controls to the first panel before you add the second one.
This is Obviously true, but when you are starting a project, and do not know the scope of whisess or needs, than you need to alter the controls on a panel.

The work around to this I found in these methodes:
1) If you add the second panel, VS adds it to the first panel, instead of the Form.
So go to Form1.Designer.cs and find "this.Controls.Add(this.panel2);" and remove it from the controls list of Panel1 and add it to the controls list of the Form.
C#
//
// panel1
//
this.panel1.Controls.Add(this.panel2);

Remove and place in section Form1
C#
// 
// Form1
//
this.Controls.Add(this.panel1);
this.Controls.Add(this.panel2);


2) If you want to work on panel 1 but now see the panel 2 in the Form.cs[Design], just alter the sequense the panels are listed in the control list of the Form:
This shows the panel 1 as active panel:
C#
//
// Form1
//
this.Controls.Add(this.panel1);
this.Controls.Add(this.panel2);


This shows the panel 2 as active panel:
C#
//
// Form1
//
this.Controls.Add(this.panel2);
this.Controls.Add(this.panel1);
Posted
Updated 22-Dec-13 0:36am
v2

You just drag and drop the textbox control on the panel. That's it!

There is no "switching" between Form or Panel layout in the IDE.
 
Share this answer
 
Comments
Maciej Los 16-Dec-13 15:22pm    
Short and to the point!
+5!
Giancarlo39 16-Dec-13 18:34pm    
Hi Dave,

Thank you for your answer.

But if I drag the textbox to the panel it's visable in both the main form and panel.
So how can I only set it visable in the main form?
After adding a panel, the main form isn't visable anymore.
There for I'm a bit puzzled.
Dave Kreskowiak 16-Dec-13 18:52pm    
No, it's not. A control cannot have two parent containers. It's visible in either one, but not the other at the same time.
idenizeni 16-Dec-13 18:53pm    
Do you have the Dock or Anchor properties set on the panel? The panel is nothing more than a child control on the form, like any control on the form. If you have it set to use Dock or Anchor to fill the whole form then you will not see other controls on the form. As for adding the text box to a specific panel, you just need to drag the textbox onto the panel you want it to be a child of.
Giancarlo39 17-Dec-13 2:17am    
Hi Idenizeni,

I made the panel have the Dock: fill property.
By means of the menustrip I wanted to have a settings panel, and the main function on the default Form.
By giving the panel Dock: fill, the panel filled (obviously) the entire canvas of the form.
So by dragging a tekst field to the canvas, it's actually viable in both the form and the panel.
I'm using VS2013 express.

As the panel fills the whole canvas I was expecting to either be able to slect the form or a panel to edit.
Or have a property at the textbox (or any control for that matter) the set it to the form or panel.

As it turns out it seems like there is not possability to switch, and there for I do not understand how I'm able to edit the Form, after a panel => Dock: fill is been altered.
Or how I am able to have the tekst box in both the panel and form.
Please, read this tutorial: Creating Your First C# Application[^]

For further information, please see: Creating Your First Visual C# Application[^]
 
Share this answer
 
Comments
Giancarlo39 16-Dec-13 18:35pm    
Hi Maciej,

As I'm new to C# I'm trying to find the answers online, and when I get stuck, I ask my question on a forum.

I'm very happy that you can past links, but I wasn't actually looking for a link, but a clarification of my mis understanding of the panel function.
If you think that my question is too stupid to be answered, please don't bother.
If you enjoy helping people with far less knowledge as your self please enlighten me with your fast understanding.
Because that is what forums are ment for (not for getting high responce rates).
Maciej Los 17-Dec-13 1:53am    
There is no stupid question. There are stupid answers - sometimes ;)
I'm not sure about your issue, that's why i provided some links.
Please, provide more details and i promise to update my answer.
Cheers!
Maciej
Giancarlo39 17-Dec-13 2:17am    
Hi Maciej,

See update above.
in last add your control to panel dynamically

this.panel1.controls.add(textbox1);
 
Share this answer
 
try this code...

C#
Checkbox cbx = new CheckBox();
                cbx.Location = new Point(15, y);
                cbx.Text = "Newcbx";
                cbx.AccessibleName = "cbx1";
                pnl_controls.Controls.Add(cbx);


pnl_controls is panel

happy coding
 
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