I have six panels, each panel contains a textbox and its label. Each panel has a checkbox associated with it on the form, but outside of its respective panel. I only want the panels whos checkboxes are checked to be visible. If another checkbox is checked, I want that respective panel to be displayed directly under the last panel displayed. Once that checkbox is unchecked, I want the panel to disapear, and all the other panels that come after, to move up. I am going crazy trying to get the code right, and I thought maybe someone has already done this as could give me an idea on how to get all these panels to apear and dissapear, and reposition according to what panels are showing and where they are. Thanks.

Dynamically moving controls
helen b
if it works, mark the answer.
now. . . which lines of code don't you understand. I am happy to explain. Someone explained it to me, once.
"Give someone a fish, they eat today; teach 'em to fish, they eat forever."
Robsterw
Im not sure that is what I was looking for. Lets say the user chooses 3 check boxes, therefore displaying 3 textboxes
1
2
3
If then he/she decided to take 2 away, I need 3 to move up
1
3
Then if they add another I need it to be added like this
1
3
4
And if 1 is taken away I want the rest to move up
3
4
Make sence I got it where I can put one after the other, and even remove some, but eventually they start appearing on top of one another. Any ideas should I put my code up Thanks.
bkannappan
sotto
Aceofspades
Shane Meeks
Kvltv
lorifajose
n-sankar
drop a panel (panel1) on your form. . . dock left
drop a panel (panel2) on panel1 . . . dock top
drop a label (label1) on panel2
click to select panel2, press [ctrl]+c to copy
click in panel1 (outside of panel2) and press [ctrl]+v 5 times to paste panels 3 through 7
drop 6 check boxes (checkbox1-6) on the right side of your form (not on panel1)
select all the checkboxes and go to the events tab on the properties window. double click in the CheckedChanged entry and add this code:
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox cb = sender as CheckBox;
if (cb == null) return;
Panel pnl = cb.Tag as Panel;
if (pnl == null) return;
pnl.Visible = cb.Checked;
for (int i = 0 ; i < panel1.Controls.Count; i++)
while(!
panel1.Controls.IndexOf(panel1.Controls[ i ]).Equals(panel1.Controls[ i ].Tag))
panel1.Controls.SetChildIndex(panel1.Controls[ i ],Convert.ToInt32(panel1.Controls[ i ].Tag));
}
and add this override to onLoad:
protected override void OnLoad(EventArgs e)
{
checkBox1.Tag = panel2;
checkBox2.Tag = panel3;
checkBox3.Tag = panel4;
checkBox4.Tag = panel5;
checkBox5.Tag = panel6;
checkBox6.Tag = panel7;
panel2.Tag = 5;
panel3.Tag = 4;
panel4.Tag = 3;
panel5.Tag = 2;
panel6.Tag = 1;
panel7.Tag = 0;
base.OnLoad(e);
}
complete code here
Srefae
sorry that is is vs 2005.
the OnLoad sets the Tag property of the CheckBox to the panel it manages and sets the Tag property of the panel to the index it should have in the contorls collection of panel1. This is necessary because toggling visible on a docked control will change its position in its parents Controls collection if the toggled control is docked in the parent.
all the handlers for the checkboxes checked event are pointed at the same handler. In this handler, sender is cast as a checkbox cb. cb's tag is cast to a panel, pnl. pnl's visible is set to the state of cb's checked. Then I iterate through the controls collection of panel1, and set the index of the contained controls to the value of its Tag (Converted to an int32 as Tag is an object property) This puts the panels in proper order on the panel.
the onLoad override could be made short like this:
protected override void OnLoad(EventArgs e)
{
checkBox1.Tag = panel2;
checkBox2.Tag = panel3;
checkBox3.Tag = panel4;
checkBox4.Tag = panel5;
checkBox5.Tag = panel6;
checkBox6.Tag = panel7;
foreach(Control ctrl in panel1.Controls)
ctrl.Tag = panel1.Controls.IndexOf(ctrl);
base.OnLoad (e);
}
here is it in vs2003
data_art
PeterZ
IainMc
merwinp
Control.BringToFront Method
Only the topmost panel is visible iff they share the same size and location.