using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace ComboBoxEx
{
public partial class UserControl1 : UserControl
{
public static int checkSize = 20;
public static string[] checkBoxList = new string[checkSize];
Panel panel1;
CheckBox selectAll = new CheckBox();
public UserControl1()
{
InitializeComponent();
}
private void btnSelect_Click(object sender, EventArgs e)
{
//
for (int j = 0; j < checkBoxList.Length; j++)
{
checkBoxList[j] = "java" + j;
}
// 控件隱藏
txtExtension.Visible = false;
btnSelect.Visible = false;
// 控件大小設置
Size = new System.Drawing.Size(450, 70 + checkBoxList.Length / 5 * 35);
//
// panel1
//
panel1.SuspendLayout();
panel1.BackColor = System.Drawing.SystemColors.ButtonHighlight;
panel1.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
panel1.Location = new System.Drawing.Point(0, 0);
panel1.Name = "panel1";
panel1.Size = new System.Drawing.Size(450, 70 + checkBoxList.Length / 5 * 35);
panel1.Visible = true;
// 動態生成Checkbox
for (int i = 0; i < checkBoxList.Length; i++)
{
CheckBox ckb = new CheckBox();
ckb.Text = checkBoxList[i];
ckb.AutoSize = true;
ckb.Location = new System.Drawing.Point(30 + 70*(i%5), 20 + i/5*35);
ckb.Name = checkBoxList[i];
ckb.Size = new System.Drawing.Size(80, 16);
ckb.UseVisualStyleBackColor = true;
panel1.Controls.Add(ckb);
}
//
// selectAll
//
selectAll = new CheckBox();
selectAll.Text = "SelectAll";
selectAll.CheckedChanged += new EventHandler(selectAll_CheckedChanged);
selectAll.AutoSize = true;
selectAll.Location = new System.Drawing.Point(120, 35 + checkBoxList.Length / 5 * 35);
panel1.Controls.Add(selectAll);
//
// btnOK
//
Button btnOK = new System.Windows.Forms.Button();
btnOK.Name = "btnOK";
btnOK.Size = new System.Drawing.Size(40, 25);
btnOK.TabIndex = 8;
btnOK.Text = "OK";
btnOK.UseVisualStyleBackColor = true;
btnOK.Click += new System.EventHandler(btnOK_Click);
btnOK.Location = new System.Drawing.Point(200, 30 + checkBoxList.Length / 5 * 35);
panel1.Controls.Add(btnOK);
Controls.Add(panel1);
panel1.ResumeLayout(false);
panel1.PerformLayout();
}
/// <summary>
/// OKボタン按下
/// </summary>
private void UserControl1_Load(object sender, EventArgs e)
{
this.Size = new System.Drawing.Size(162, 20);
this.panel1 = new System.Windows.Forms.Panel();
panel1.Visible = false;
}
/// <summary>
/// 控件初期化
/// </summary>
private void btnOK_Click(object sender, EventArgs e)
{
// 控件大小設置
Size = new System.Drawing.Size(162, 20);
// 控件顯示
txtExtension.Visible = true;
btnSelect.Visible = true;
// 全選
CheckBox all = sender as CheckBox;
string extension = "";
bool isFirst = true;
// 文本框清空
txtExtension.Text = "";
foreach (Control ctl in panel1.Controls)
{
if (ctl is CheckBox)
{
CheckBox chk = ctl as CheckBox;
if (chk.Checked)
{
if (!isFirst)
{
extension = extension + ',';
}
extension = extension + chk.Name;
isFirst = false;
}
}
}
// 文本框賦直
txtExtension.Text = extension;
}
/// <summary>
/// 全選Click
/// </summary>
void selectAll_CheckedChanged(object sender, EventArgs e)
{
CheckBox all = sender as CheckBox;
foreach (Control ctl in panel1.Controls)
{
if (ctl is CheckBox)
{
CheckBox chk = ctl as CheckBox;
chk.Checked = all.Checked;
}
}
}
}
}