日期:2014-05-18 浏览次数:20833 次
private void checkBox1_CheckedChanged( object sender,EventArgs e) { String text1=“你的爱好是:”; //checkBox1是标题为"音乐"的多选框控件 if(checkBox1.Checked) text1=text1+checkBox1.Text; //checkBox2是标题为"文学"的多选框控件 if(checkBox2.Checked) text1+=checkBox2.Text; label1.Text=text1; }
private void checkBox1_CheckedChanged(object sender, EventArgs e) { if (checkBox1.Checked) label1.Text += checkBox1.Text; else { label1.Text = label1.Text.Replace(checkBox1.Text,""); } } private void checkBox2_CheckedChanged(object sender, EventArgs e) { if (checkBox2.Checked) label1.Text += checkBox2.Text; else { label1.Text = label1.Text.Replace(checkBox2.Text,""); } }
------解决方案--------------------
正确
我提供另一种方法,可以封装以备后用
private void showText(string title) { string str = string.Empty; if (checkBox1.Checked && !checkBox2.Checked) str = checkBox1.Text; if (checkBox2.Checked && !checkBox1.Checked) str = checkBox2.Text; if (checkBox1.Checked && checkBox2.Checked) str = checkBox1.Text + checkBox2.Text; label1.Text = title + str; } private void checkBox1_CheckedChanged(object sender, EventArgs e) { showText("你的爱好是:"); } private void checkBox2_CheckedChanged(object sender, EventArgs e) { showText("你的爱好是:"); }