How do I add padding, or some space between the textboxes when using dockstyle.top property?
for(int i =0; i< 10; i++) {
textboxes[i] = new TextBox();
textboxes[i].Dock = DockStyle.Top;
mypanel.Controls.Add(textboxes[i]);
}
The code above puts textboxes right beneath each other. Can't figure this out without using mass panels or fixed positioning. How to do the following?
1) I would like to add around 10-20pixels between boxes.
2) How to change size (height,width) of the textboxes, since when using dockstyle.top it ignores the size commands ?
With DockStype.Top you can't change the width of your TextBoxes, cause they are docked. You can only change the height. But to change the height of a TextBox you have to set the
Multiline = true
beforehand.To get the space between the different boxes you have to put each TextBox within a panel, set the
TextBox.Dock = Fill
, thePanel.Dock = Top
and thePanel.Padding = 10
. Now you have some space between each TextBox.Sample Code
What i forgot, you can also give a try to the FlowLayoutPanel. Just remove the
DockStyle.Top
from the panels and put them into the FlowLayoutPanel. Also you should set the FlowDirection to TopDown. Maybe this can also help you to solve your problem, too.I know where you're coming from, this is especially frustrating after returning to WinForms from WPF.
I would suggest using a TableLayoutPanel, in which each TextBox would get its own cell, and adjusting the properties of the cells. This should solve both your padding and size problems.
Another alternative would be to use some more complex layout controls, such as the DevExpress ones (not free).
Another work around that suits smaller layouts is to just add a
Label
control afterwards also docked to theTop
, which is not AutoSized,Text=" "
,Height=your padding
. This is quite useful for the odd bit of padding when using the designer.