Blazi
Blazi

Reputation: 1011

How to make controls stay in a distance of each other when adding them programmatically?

I have a panel, which I add controls programmatically to it. I want each control stay in a far from other controls and not stay on top of them.

for this purpose I can calculate a position for each control based on Panel's size, but it seems a bit odd.

Is there a way to make controls be added in a line and when it ended they be added in another line?

Upvotes: 2

Views: 1329

Answers (3)

Moha Dehghan
Moha Dehghan

Reputation: 18472

You have a few options. You can use one of the containers such as FlowLayoutPanel or TableLayoutPanel. You can also nest them in each other. And you have to set the Margin property for each control you add to the containers.

Sadly the Windows Forms technology lacks on this part a little, while WPF has a very rich layout system. Even somethings like Margin doesn't always work as expected.

Upvotes: 1

Michael G
Michael G

Reputation: 6755

I believe the WrapPanel class does what you're describing in WPF. Or the FlowLayoutPanel in WinForms.

Upvotes: 2

Adam Plocher
Adam Plocher

Reputation: 14243

You can use a FlowLayoutPanel to achieve what you're describing. It's under Containers in the ToolBox. Set the direction to horizontal and it will flow from left to right, and wrap when it needs to.

Upvotes: 6

Related Questions