C# CheckBox复选框
using System.Windows.Forms;
namespace WinFormTest
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
侍喊 this.checkBox1.CheckedChanged += checkBoxs_CheckedChanged;
this.checkBox2.CheckedChanged += checkBoxs_CheckedChanged;
this.checkBox3.CheckedChanged += checkBoxs_CheckedChanged;
}
private void checkBoxs_CheckedChanged(object sender, EventArgs e)
{
string txtToShow = "";
if (this.checkBox1.Checked)
{
txtToShow += "1";
}
if (this.checkBox2.Checked)
{
txtToShow += "2";
}
if (this.checkBox3.Checked)
{
txtToShow += "3";
}
this.textBox1.Text = txtToShow;
}
}
}
赞同protorock0627的搞法,可以如上修改一下,更规整
更高端的搞法是:用面向对象,设计CheckBoxItem类和CheckBoxs类。
=========================CheckBoxs========================
using System;
using System.Collections.Generic;
using System.Windows.Forms;
namespace WinFormTest
{
public class CheckBoxs
{
private static CheckBoxs Instance = null;
private static List<CheckBoxItem> items;
private CheckBoxs()
{
}
public static CheckBoxs GetInstance()
{
if (Instance == null)
{
Instance = new CheckBoxs();
items = new List<CheckBoxItem>();
}
return Instance;
}
public static CheckBoxs GetInstance(TextBox tb)
{
GetInstance();
Instance.TextBoxToShowMessage = tb;
return Instance;
}
public TextBox TextBoxToShowMessage
{
get;
set;
}
private void checkBoxs_CheckedChanged(object sender, EventArgs e)
{
this.ChangeState(sender);
this.ChangeMessageShown();
}
private void ChangeMessageShown()
{
string TextToShow = "";
滑孝 foreach (CheckBoxItem cbi in items)
{
if (cbi.IsNeedShow)
{
TextToShow += cbi.TextToShow;
}
}
this.TextBoxToShowMessage.Text = TextToShow;
}
private void ChangeState(object sender)
{
foreach (CheckBoxItem cbi in items)
{
if (sender.Equals(cbi.Control))
{
cbi.IsNeedShow = !cbi.IsNeedShow;
}
}
}
public void Add(CheckBoxItem cbi)
{
items.Add(cbi);
this.ChangeMessageShown();
cbi.Control.CheckedChanged+=this.checkBoxs_CheckedChanged;
}
public void Insert(int index, CheckBoxItem cbi)
{
信谈稿 items.Insert(index, cbi);
this.ChangeMessageShown();
cbi.Control.CheckedChanged += this.checkBoxs_CheckedChanged;
}
public void Remove(CheckBoxItem cbi)
{
items.Remove(cbi);
this.ChangeMessageShown();
cbi.Control.CheckedChanged -= this.checkBoxs_CheckedChanged;
}
public void Remove(CheckBox cb)
{
for(int i=0;i<items.Count;i++)
{
if (items[i].Control.Equals(cb))
{
items[i].Control.CheckedChanged -= this.checkBoxs_CheckedChanged;
items.RemoveAt(i);
break;
}
}
this.ChangeMessageShown();
}
public static CheckBoxs operator +(CheckBoxs cbs,CheckBoxItem cbi)
{
cbs.Add(cbi);
return Instance;
}
public static CheckBoxs operator -(CheckBoxs cbs, CheckBoxItem cbi)
{
cbs.Remove(cbi);
return Instance;
}
public static CheckBoxs operator -(CheckBoxs cbs, CheckBox cb)
{
cbs.Remove(cb);
return Instance;
}
}
}
=========================CheckBoxItem========================
using System.Windows.Forms;
namespace WinFormTest
{
public class CheckBoxItem
{
public CheckBoxItem(CheckBox cb, string TxtToShow)
{
this.Control = cb;
this.TextToShow = TxtToShow;
if (cb.Checked)
this.IsNeedShow = true;
}
public CheckBox Control { get; set; }
public string TextToShow { get; set; }
public bool IsNeedShow
{
get;
set;
}
}
}
=========================frmMain========================
using System.Windows.Forms;
namespace WinFormTest
{
public partial class frmMain : Form
{
private CheckBoxs cbs;
private bool isAlreadyRemoved;
public frmMain()
{
InitializeComponent();
this.cbs=CheckBoxs.GetInstance(this.textBox1);
this.cbs += new CheckBoxItem(this.checkBox1, "1");
this.cbs += new CheckBoxItem(this.checkBox2, "2");
this.cbs += new CheckBoxItem(this.checkBox3, "3");
}
private void frmMain_Click(object sender, System.EventArgs e)
{
//CheckBoxs.Add与CheckBoxs.Remove测试
if(this.isAlreadyRemoved)
{
this.cbs.Insert(0, new CheckBoxItem(this.checkBox1, "1"));
MessageBox.Show("this.checkBox1已被重新加入");
}
else
{
this.cbs -= this.checkBox1;
MessageBox.Show("this.checkBox1已被移除");
}
this.isAlreadyRemoved = !this.isAlreadyRemoved;
}
}
}
这样你任何时期都可以加入或移除对应的项,还可以随意更改用来显示消息的TextBox控件。
把你的窗体增加一个Paint重绘事件
然后把下面的代码放到这个Paint重绘事件中就可以了
if (checkBox1.Checked) {
if (checkBox1.Checked &&迹让 checkBox2.Checked)
{
if (checkBox1.Checked && checkBox2.Checked && checkBox3.Checked)
{
textBox1.Text = "123";
}
else
{
textBox1.Text = "12";
}
纤宴 }
else
{
textBox1.Text = "1";
}
毁州银 }
else if (checkBox2.Checked)
{
if (checkBox2.Checked && checkBox3.Checked)
{
textBox1.Text = "23";
}
else
{
textBox1.Text = "2";
}
}
else if (checkBox3.Checked)
{
textBox1.Text = "3";
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
ShowCheckText();
}
private void checkBox2_CheckedChanged(object sender, EventArgs e)
{
ShowCheckText();
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
闹毕 举弯枯ShowCheckText();
}
private void ShowCheckText()
{
string s = string.Empty;
if (checkBox1.Checked)
{
s = "1";
}
if (checkBox2.Checked)
{
s += "2";
}
if (checkBox3.Checked)
正洞 {
s += "3";
}
textBox1.Text = s;
}
那如果TextBox框里要是显示别的呢?比如选1显示7365;选2显示8986;选1和2时显示9807呢?
要显示别的,只要修改ShowCheckText就可以了
private void ShowCheckText()
{
string s = string.Empty;
if (checkBox1.Checked)
{
s = "7365";
}
if (checkBox2.Checked)
{
s += "8986;
}
if (checkBox3.Checked)
{
s += "3";
}
textBox1.Text = s;
}
如果你要实现“选1和2时显示9807”或者其他类似的组合,也的修改这个方法,例如
private void ShowCheckText()
{
string s = string.Empty;
if(checkBox1.Check && checkBox2.Checked)
{
s="9807";
}
else
{
if (checkBox1.Checked)
{
s = "7365";
}
if (checkBox2.Checked)
{
s += "8986;
}
}
if (checkBox3.Checked)
{
s += "3";
}
textBox1.Text = s;
}
namespace CheckBoxPractise
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
public partial class 扒空物Form1 : Form
{
private readonly Dictionary<string, string> checkValueDictionary;
public Form1()
{
InitializeComponent();
this.checkValueDictionary = new Dictionary<string, string>
{
{this.checkBox1.Name,"选项一"},
{this.checkBox2.Name,"选项二"},
{this.checkBox3.Name,"选项三"}
};
亏孙 }
private void checkBox_CheckedChanged(object sender, EventArgs e)
{
this.DisplayChoices();
}
private void DisplayChoices()
{
春液 var choiceBuilder = new StringBuilder();
foreach (var checkBox in this.groupBox1.Controls.OfType<CheckBox>().Select(control => control as CheckBox).Where(checkBox => checkBox.Checked))
{
choiceBuilder.AppendFormat("{0} ",this.checkValueDictionary[checkBox.Name]);
}
this.textBoxChoices.Text = choiceBuilder.ToString();
}
}
}