111,097
社区成员




using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace WindowsFormsApplication6
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void SetCheckBox(GroupBox gb,Label lb)
{
lb.Text = lb.Text + "----" + gb.Location.X.ToString()+"/"+gb.Location.Y.ToString();
for (int a = gb.Location.Y + 12; a < gb.Location.Y + gb.Size.Height - 22; a = a + 22)
{
for (int b = gb.Location.X + 12; b < gb.Location.X + gb.Size.Width - 114; b = b + 112)
{
CheckBox cb = new CheckBox() { Text = "GB_" + b.ToString() + "_" + a.ToString(), Name = "GB_" + b.ToString() + "_" + a.ToString() };
cb.Location = new System.Drawing.Point(b, a);
cb.Size = new System.Drawing.Size(108, 16);
gb.Controls.Add(cb);
lb.Text = lb.Text + "----" + cb.Name;
}
}
}
private void Form1_Load(object sender, EventArgs e)
{
SetCheckBox(this.groupBox1,this.label1);
SetCheckBox(this.groupBox2, this.label2);
}
}
}
/// <summary>
/// 向GroupBox中添加CheckBox
/// </summary>
/// <param name="groupBox">指定的GroupBox</param>
/// <param name="colOfUIItems">CheckBox属性的集合</param>
/// <param name="evnForCheckBox">CheckBox要响应的点选事件</param>
private void SetCheckBox(GroupBox groupBox, IEnumerable<UIItems> colOfUIItems,EventHandler evnForCheckBox)
{
//存储CheckBox对象
Queue<CheckBox> que = new Queue<CheckBox>();
var t = from d in colOfUIItems
select new CheckBox
{
Name = d.ItemName,
Text = d.Text,
};
//CheckBox添加事件
foreach (var item in t)
{
item.CheckedChanged +=evnForCheckBox;
que.Enqueue(item);
}
//指定CheckBox在GroupBox中的坐标,坐标为相对坐标
for (int y =20; y <groupBox.Size.Height - 22; y = y + 22)
{
for (int x =12; x <groupBox.Size.Width - 16; x = x + 112)
{
if (que.Count > 0)
{
CheckBox cb = que.Dequeue();
cb.Location = new System.Drawing.Point(x, y);
cb.Size = new System.Drawing.Size(108, 16);
groupBox.Controls.Add(cb);
}
}
}
}