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

本文详细介绍了如何根据参数设置将按钮按照指定行数进行排列并调整大小,同时考虑了间隔大小的影响。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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

 

转载于:https://www.cnblogs.com/zzfstudy/p/5164041.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值