How do I orderly generate controls underneath eachother?
I have a class which generates a form and controls. The controls vary between buttons, radio controls, check boxes and labels. The idea is to neatly create the form with a label above each control. The problem is that I cannot seem to find the formula or way to neatly organise/space them. It works fine when I have just text boxes, but I'm not sure how to handle larger controls, like check box lists. Here's an example of how I handle text boxes:
case "Text":
TextBox tbx = new TextBox();
tbx.Name = df.Value.Name;
tbx.Text = (df.Value.DefValue != null) ? df.Value.DefValue : "";
开发者_如何学C tbx.Location = new Point(lbl.Location.X, lbl.Location.Y + 20);
f.Controls.Add(tbx);
break;
Mind that this is all in a foreach
loop. This is the part that precedes it (label):
if (i == 0)
{
lbl.Location = new Point(10, 10);
}
else
{
lbl.Location = new Point(10, (i * 50) + 10);
}
This neatly sorts the text boxes and labels out with an even spacing. Can anyone offer me some advice on how to handle different controls? I want to place them underneath eachother but keep at least 10 pixels spacing from the bottom of each control to the top of the next label.
Thank you in advance.
Use a tablelayout. You can take a look at how the designer does it in the code behind file.
For the spacing, fill in the "margin" property of your controls. 5 on top and 5 at the bottom should do it.
精彩评论