Link to home
Start Free TrialLog in
Avatar of rwallacej
rwallacej

asked on

VB.net FlowLayoutPanel - centering controls

I've got a FlowLayoutPanel to which custom controls are added as the application runs.

Can the controls be arranged so they are CENTRED on the FlowLayoutPanel (just now they're left aligned)  - centring the controls would look better

Thanks
Avatar of Jeff Certain
Jeff Certain
Flag of United States of America image

My initial reaction is "no"

The whole point of the flow layout is to just start laying out controls from left to right until you're done.

What you may be able to do is resize the layout panel and position it such that it appears that you've achieved centering.
Of the head, it could be done, with Padding property. If controls are of  same size, then you know how many are located in one line. Free space on the right side is easy to calculate, and you need to divide it by 2 and set such value for Panel.Padding.Left property

However, if size of controls is not the same, then my first guess is that you would be needing to loop through all controls, watch for their Location.Y property, when its the same, it means controls are on the same line. When you come to the last control

Panel.Size.Width-LastControl.Location.x-LastControl.Size.Width=FreeSpaceOnTheRightSide

the rest should be as mentioned above

Maybe it  looks confusing, but I dont think there is another way. :)
ASKER CERTIFIED SOLUTION
Avatar of Priest04
Priest04
Flag of Serbia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I don't know why rwallacej hasn't responded, but I am positive that my solution works (have tested it and it works).

Goran
Avatar of rwallacej
rwallacej

ASKER

thanks for feedback
you are welcome