日期:2014-05-18 浏览次数:20790 次
private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e) { this.textBox1.Text=string.Empty; foreach (var item in this.checkedListBox1.CheckedItems) { this.textBox1.Text += item.ToString() + "\r\n"; } }
------解决方案--------------------
所有Checkbox都调用这一个事件
private void checkBox4_Click(object sender, EventArgs e)
{
string txt = "";
foreach (Control item in Controls)
{
if (item is CheckBox)
{
if (((CheckBox)item).Checked)
txt += ((CheckBox)item).Text+"\r\n";
else
txt = txt.Replace(((CheckBox)item).Text, "");
}
}
richTextBox1.Text = txt;
}
------解决方案--------------------
List<string> hbs = new List<string>(); string hb = string.Empty; private void checkBox1_CheckedChanged(object sender, EventArgs e) { hb = string.Empty; if (this.checkBox1.Checked) hbs.Add(this.checkBox1.Text); else hbs.Remove(this.checkBox1.Text); foreach (string s in hbs) { hb += s; } this.textBox1.Text = hb; } private void checkBox2_CheckedChanged(object sender, EventArgs e) { hb = string.Empty; if (this.checkBox2.Checked) hbs.Add(this.checkBox2.Text); else hbs.Remove(this.checkBox2.Text); foreach (string s in hbs) { hb += s; } this.textBox1.Text = hb; }
------解决方案--------------------
写完了,你看一下:
public partial class Form1 : Form { public Form1() { InitializeComponent(); //注意,所有的checkBox都绑定一个事件就可以了 checkBox1.CheckedChanged+=new EventHandler(checkBox1_CheckedChanged); checkBox2.CheckStateChanged += new EventHandler(checkBox1_CheckedChanged); checkBox3.CheckedChanged += new EventHandler(checkBox1_CheckedChanged); checkBox4.CheckedChanged += new EventHandler(checkBox1_CheckedChanged); } List<string> list = new List<string>(); private void checkBox1_CheckedChanged(object sender, EventArgs e) { CheckBox cb = sender as CheckBox; if (cb != null) { if (cb.Checked) { list.Add(((CheckBox)sender).Text); } else { list.Remove(((CheckBox)sender).Text); } ShowTxt(list); } } private void ShowTxt(List<string> list) { if (list == null || list.Count == 0) return; StringBuilder str = new StringBuilder(); foreach (string s in list) { str.Append(s); str.Append("\r\n"); } textBox1.Text = str.ToString(); } }
------解决方案--------------------