怎样遍历Form中所有自动生成的CheckedListBox(急)
如题:
怎样遍历Form中所有自动生成的CheckedListBox
遍历想查看每个CheckedListBox有多少被选中,多少没选!
请各位帮忙 开解
[解决办法]
foreach (CheckBox cb in Form.Controls)
{
if (cb.Checked)
{
selectTypeCount++;
}
}
if (selectTypeCount == 0)
{
error.SetError(groupBox1, "请至少选择一种图片类型 ");
}
else
{
error.SetError(groupBox1, " ");
}
本人的程序代码。我用的是CheckBox,你用的是CheckedListBox,我想这个不难改成你希望的代码。
[解决办法]
我以前是这样解决的,改变控件的外观,稍加修改应该适用于你的要求,不知道有没有更好的办法。
foreach (Control c in this.Controls)
{
string s = c.GetType().ToString();
switch (s)
{
case "System.Windows.Forms.Button ":
{
Button b = (Button)c;
if (b.FlatStyle == FlatStyle.Flat)
{
b.FlatStyle = FlatStyle.Standard;
}
else
{
b.FlatStyle = FlatStyle.Flat;
}
break;
}
case "System.Windows.Forms.TextBox ":
{
TextBox t = (TextBox)c;
if (t.BorderStyle == BorderStyle.FixedSingle)
{
t.BorderStyle = BorderStyle.Fixed3D;
}
else
{
t.BorderStyle = BorderStyle.FixedSingle;
}
break;
}
}
}
[解决办法]
public class Form1 : Form
{
private GroupBox groupBox1;
private CheckedListBox checkedListBox2;
private GroupBox groupBox2;
private CheckedListBox checkedListBox3;
private CheckedListBox checkedListBox1;
private TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.IContainer components = null;
private Label label1;
/// <summary>
/// 找到CheckedListBox的数量
/// </summary>
private int m_ctrlCount;
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
/// <param name= "disposing "> 如果应释放托管资源,为 true;否则为 false。 </param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.checkedListBox2 = new System.Windows.Forms.CheckedListBox();
this.checkedListBox3 = new System.Windows.Forms.CheckedListBox();
this.textBox1 = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// groupBox1
//
this.groupBox1.Controls.Add(this.checkedListBox2);
this.groupBox1.Controls.Add(this.groupBox2);
this.groupBox1.Location = new System.Drawing.Point(12, 84);
this.groupBox1.Name = "groupBox1 ";
this.groupBox1.Size = new System.Drawing.Size(356, 225);
this.groupBox1.TabIndex = 0;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "groupBox1 ";
//
// groupBox2
//
this.groupBox2.Controls.Add(this.checkedListBox3);
this.groupBox2.Location = new System.Drawing.Point(25, 83);
this.groupBox2.Name = "groupBox2 ";
this.groupBox2.Size = new System.Drawing.Size(305, 116);
this.groupBox2.TabIndex = 0;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "groupBox2 ";
//
// checkedListBox1
//
this.checkedListBox1.FormattingEnabled = true;
this.checkedListBox1.Location = new System.Drawing.Point(12, 12);
this.checkedListBox1.Name = "checkedListBox1 ";
this.checkedListBox1.Size = new System.Drawing.Size(120, 52);
this.checkedListBox1.TabIndex = 1;
//
// checkedListBox2
//
this.checkedListBox2.FormattingEnabled = true;
this.checkedListBox2.Location = new System.Drawing.Point(25, 20);
this.checkedListBox2.Name = "checkedListBox2 ";
this.checkedListBox2.Size = new System.Drawing.Size(120, 52);
this.checkedListBox2.TabIndex = 2;
//
// checkedListBox3
//
this.checkedListBox3.FormattingEnabled = true;
this.checkedListBox3.Location = new System.Drawing.Point(19, 29);
this.checkedListBox3.Name = "checkedListBox3 ";
this.checkedListBox3.Size = new System.Drawing.Size(120, 52);
this.checkedListBox3.TabIndex = 2;
//
// textBox1
//
this.textBox1.BackColor = System.Drawing.SystemColors.Control;
this.textBox1.BorderStyle = System.Windows.Forms.BorderStyle.None;
this.textBox1.Location = new System.Drawing.Point(155, 32);
this.textBox1.Multiline = true;
this.textBox1.Name = "textBox1 ";
this.textBox1.ReadOnly = true;
this.textBox1.Size = new System.Drawing.Size(213, 32);
this.textBox1.TabIndex = 2;
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(150, 12);
this.label1.Name = "label1 ";
this.label1.Size = new System.Drawing.Size(53, 12);
this.label1.TabIndex = 3;
this.label1.Text = "已找到: ";
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(380, 344);
this.Controls.Add(this.label1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.checkedListBox1);
this.Controls.Add(this.groupBox1);
this.Name = "Form1 ";
this.Text = "Form1 ";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
public Form1()
{
InitializeComponent();
this.m_ctrlCount = 0;
}
protected override void OnLoad(EventArgs e)
{
SearchControl(this.Controls);
base.OnLoad(e);
}// end void
/// <summary>
///
/// </summary>
/// <param name= "controls "> </param>
private void SearchControl(System.Windows.Forms.Control.ControlCollection controls)
{
foreach (Control control in controls)
{
if (control is CheckedListBox)
{
this.m_ctrlCount++;
this.textBox1.Text = this.m_ctrlCount.ToString( "d ");
}// end if
else
{
System.Windows.Forms.Control.ControlCollection NextControls = control.Controls;
if (NextControls.Count > 0)
{
SearchControl(NextControls);
}// end if
}// end if
}// end foreach
}// end void
}