Button控件是一个常用的控件,网上的资料也比较多,我这里也是参考Winform开发常用控件之Checkbox和CheckedListBox的文章来学习CheckBox的使用,Demo例程就是参考他的文章,来实现全选和反选的功能
1.界面布局
界面布局如下
这里我们需要两个label,四个Checkbox,和一个button,详细的布局代码就不贴了
需要注意的一点是,Controls默认是按照你添加控件的顺序来添加的,如果我们想顺序显示(篮球 -> 足球 -> 排球)
就必须要手动修改添加顺序,比如这样
//
// FormMain
//
this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(290, 333);
this.Controls.Add(this.checkBox4);//全选
this.Controls.Add(this.button1);
this.Controls.Add(this.label2);
this.Controls.Add(this.checkBox1);//篮球
this.Controls.Add(this.checkBox2);//足球
this.Controls.Add(this.checkBox3);//排球
this.Controls.Add(this.label1);
this.Name = "FormMain";
this.Text = "Form1";
this.ResumeLayout(false);
this.PerformLayout();
我们手动把添加顺序改为篮球 -> 足球 -> 排球,这样在遍历显示的时