码迷,mamicode.com
首页 > Windows程序 > 详细

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

时间:2016-01-27 19:01:26      阅读:154      评论:0      收藏:0      [点我收藏+]

标签:

技术分享
 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 后台将指定的控件集合添加到制定容器中

标签:

原文地址:http://www.cnblogs.com/zzfstudy/p/5164041.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!