Winform 后台将指定的控件集合添加到制定容器中

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Winform 后台将指定的控件集合添加到制定容器中相关的知识,希望对你有一定的参考价值。

技术分享
 1          /// <summary>
 2         /// 把按钮按照行数分割排列
 3         /// </summary>
 4         /// <param name="ControlArry">按钮集合</param>
 5         /// <param name="control_parent">父容器</param>
 6         /// <param name="RowCount">每一行数量</param>
 7         /// <param name="ControlSize">控件大小</param>
 8         /// <param name="pad">间隔大小</param>
 9         private void ControlToControlResize(Control[] ControlArry, Control control_parent, int RowCount, Size? ControlSize, Padding pad)
10         {
11             //计算按钮相关信息
12             control_parent.Controls.Clear();
13             //列数
14             int yCount = 0; int xCount = RowCount;
15             if (ControlArry.Length < RowCount) //定义一列展示的数量大于总控件
16             {
17                 yCount = 1;
18             }
19             else
20             {
21                 yCount = ControlArry.Length % RowCount == 0 ? ControlArry.Length / RowCount : ControlArry.Length / RowCount + 1;
22             }
23             Padding ParentsPadding = control_parent.Padding;
24             Size btnSize = new System.Drawing.Size();
25             if (ControlSize != null)
26             {
27                 btnSize = (Size)ControlSize;
28             }
29             else
30             {
31                 btnSize.Width = Convert.ToInt32(Math.Floor(((double)control_parent.Width - (ParentsPadding.Left + ParentsPadding.Right)) / RowCount));
32                 btnSize.Height = Convert.ToInt32(Math.Floor(((double)control_parent.Height - (ParentsPadding.Top + ParentsPadding.Bottom)) / yCount));
33             }
34             int index = 0;
35             for (int i = 0; i < yCount; i++)//行数
36             {
37                 for (int j = 0; j < xCount; j++)//一行多少个
38                 {
39                     if (index >= ControlArry.Length)
40                     {
41                         break;
42                     }
43                     else
44                     {
45                         ControlArry[index].Size = btnSize;
46                         ControlArry[index].Padding = pad;
47                        ControlArry[index].Location = new Point(j * btnSize.Width + ParentsPadding.Left, i * btnSize.Height + ParentsPadding.Top);
48                         index++;
49                     }
50                 }
51             }
52             control_parent.Controls.AddRange(ControlArry);
53         }
View Code

 

以上是关于Winform 后台将指定的控件集合添加到制定容器中的主要内容,如果未能解决你的问题,请参考以下文章

winform用户控件动态创建添加控件timer控件控件联动

winform用户控件timer控件三级联动

c# winform 循环控件循环赋值问题?

wpf添加新控件原本的控件消失

C#winform动态添加控件

怎么用C#在winform的后台指定button控件的背景图片呢?