简体   繁体   English

以编程方式 Windows 窗体和用户控件布局

[英]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我发现以下有效: 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);分配newcontrol.Location = new System.Drawing.Point(550, offset);

and increment offset by the desired spacing each time you go through the loop.并在每次通过循环时按所需间距增加偏移量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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