Depending of XML file contents, in run time the program must create dynamically textbox and labels. I want to organize this controls as the following inside a groupbox:
[label1] [textbox1] [label2] [texbox2] [label3] [textbox]
[label4] [textbox4] [label5] [textbox5] [...]
and so.
But I don’t know how to compute the X and Y values. I have tried several solutions, but no luck. Below my current implemenation which put the label and textbox per line:
[label1] [texbox1]
[labe2] [texbox2]
and so.
//list the disiciplanas from XML file
//into combobox control.
for (int i = 0,
label_X = 20, label_Y = 20,
textbox_X = 74, textbox_Y = 20,
len = materias[0].ChildNodes.Count,
line = 0, tmp_pos = 20;
i < len;
i++,
tab_index++,
line++,
tmp_pos += 20
)
{
XmlNode materia = materias[0].ChildNodes[i];
Point label_position, textbox_position;
Label label = new Label();
TextBox textbox = new TextBox();
#if DEBUG_
if (line == 3)
{
label_X = 200 + tmp_pos;
label_Y = 25 + tmp_pos;
}
else
{
label_X += 10;
}
textbox_Y = label_Y;
#else
label_Y += 20;
textbox_Y = label_Y;
#endif
label.Text = "foo";
textbox.Size = new Size(48, 20);
textbox.Name = String.Format("nota{0}", fo.Name);
label_position = new Point();
label_position.X = label_X;
label_position.Y = label_Y;
textbox_position = new Point();
textbox_position.X = textbox_X;
textbox_position.Y = textbox_Y;
label.Location = label_position;
textbox.Location = textbox_position;
textbox.Left = 150;
textbox.TabIndex = tab_index;
notas_panel.Controls.Add(label);
notas_panel.Controls.Add(textbox);
}
I hope this is clear. Thanks in advance
The easiest way is don’t calculate them.
Create a panel and get rid of it’s border. Create the dynamic controls on the panel and set the panel to Dock Top, then call
BringToFront()(or could beSendToBack()I can never remember which way round they are to help with docking at run time.) on the panel.When you create the next lot the controls all have the same positions but their parent panel is automatically placed underneath the earlier one.
The result should look like this:
