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

2020-08-31 13:35:40 浏览数 (1)

代码语言:javascript复制
 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         }

0 人点赞