简体   繁体   中英

Windows form and user control layout programmatically

How can I dynamically add a user control to my form and making sure it is aligned properly? In other words, what's the easiest way to control how things are aligned (centered, vertically, horizontally, etc.) when adding it dynamically to my form?

I found the following that works: http://www.vcskicks.com/align-user-interface.php

One option if you want to explicitly define the location of each new control:

lets say you have a list of objects that you want to create controls for but in different instances you may have a different number of objects in the list. You can loop over the list, instantiating a new control for each item, and placing that control on the form or in another control(like a layout panel) and then explicitly assign the properties of the new control, specifically the location.

Say you want all of your dynamically added controls to be lined up vertically;

assign newcontrol.Location = new System.Drawing.Point(550, offset);

and increment offset by the desired spacing each time you go through the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM