【说明下界面上要用到的控件:1个TextBox,一个Button,16个CheckBox】
16个CheckBox依次按顺序摆好(CheckBox1~CheckBox16),我要实现的功能是:在16个CheckBox随便选中几个,然后点Button按钮,这时候TextBox就会显示相对应的正确值(选中状态为1,未选中状态为0)。
举个例子,比如我选中了16个CheckBox中两边的CheckBox(既CheckBox1和CheckBox16),那么当我点击确定的时候,TextBox上显示的值应该是1000000000000001
我的做法:string a = "";
string b = "";
string c = "";
string d = "";
string ee = "";
string f = "";
string g = "";
string h = "";
string i = "";
string j = "";
string k = "";
string l = "";
string m = "";
string n = "";
string o = "";
string p = "";
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
a = "1";
}
else
{
a = "0";
} if (checkBox2.Checked == true)
{
b = "1";
}
else
{
b = "0";
} if (checkBox3.Checked == true)
{
c = "1";
}
else
{
c = "0";
} if (checkBox4.Checked == true)
{
d = "1";
}
else
{
d = "0";
} if (checkBox5.Checked == true)
{
ee = "1";
}
else
{
ee = "0";
} if (checkBox6.Checked == true)
{
f = "1";
}
else
{
f = "0";
} if (checkBox7.Checked == true)
{
g = "1";
}
else
{
g = "0";
} if (checkBox8.Checked == true)
{
h = "1";
}
else
{
h = "0";
} if (checkBox9.Checked == true)
{
i = "1";
}
else
{
i = "0";
} if (checkBox10.Checked == true)
{
j = "1";
}
else
{
j = "0";
} if (checkBox11.Checked == true)
{
k = "1";
}
else
{
k = "0";
} if (checkBox12.Checked == true)
{
l = "1";
}
else
{
l = "0";
} if (checkBox13.Checked == true)
{
m = "1";
}
else
{
m = "0";
} if (checkBox14.Checked == true)
{
n = "1";
}
else
{
n = "0";
} if (checkBox15.Checked == true)
{
o = "1";
}
else
{
o = "0";
} if (checkBox16.Checked == true)
{
p = "1";
}
else
{
p = "0";
}
textBox1.Text = a + b + c + d + ee + f + g + h + i + j + k + l + m + n + o + p;
}感觉20行代码能完成的功能,我用了200行!! 请高手把这段代码变短或用更好的方法实现!!感激不尽!!!
16个CheckBox依次按顺序摆好(CheckBox1~CheckBox16),我要实现的功能是:在16个CheckBox随便选中几个,然后点Button按钮,这时候TextBox就会显示相对应的正确值(选中状态为1,未选中状态为0)。
举个例子,比如我选中了16个CheckBox中两边的CheckBox(既CheckBox1和CheckBox16),那么当我点击确定的时候,TextBox上显示的值应该是1000000000000001
我的做法:string a = "";
string b = "";
string c = "";
string d = "";
string ee = "";
string f = "";
string g = "";
string h = "";
string i = "";
string j = "";
string k = "";
string l = "";
string m = "";
string n = "";
string o = "";
string p = "";
private void button1_Click(object sender, EventArgs e)
{
if (checkBox1.Checked == true)
{
a = "1";
}
else
{
a = "0";
} if (checkBox2.Checked == true)
{
b = "1";
}
else
{
b = "0";
} if (checkBox3.Checked == true)
{
c = "1";
}
else
{
c = "0";
} if (checkBox4.Checked == true)
{
d = "1";
}
else
{
d = "0";
} if (checkBox5.Checked == true)
{
ee = "1";
}
else
{
ee = "0";
} if (checkBox6.Checked == true)
{
f = "1";
}
else
{
f = "0";
} if (checkBox7.Checked == true)
{
g = "1";
}
else
{
g = "0";
} if (checkBox8.Checked == true)
{
h = "1";
}
else
{
h = "0";
} if (checkBox9.Checked == true)
{
i = "1";
}
else
{
i = "0";
} if (checkBox10.Checked == true)
{
j = "1";
}
else
{
j = "0";
} if (checkBox11.Checked == true)
{
k = "1";
}
else
{
k = "0";
} if (checkBox12.Checked == true)
{
l = "1";
}
else
{
l = "0";
} if (checkBox13.Checked == true)
{
m = "1";
}
else
{
m = "0";
} if (checkBox14.Checked == true)
{
n = "1";
}
else
{
n = "0";
} if (checkBox15.Checked == true)
{
o = "1";
}
else
{
o = "0";
} if (checkBox16.Checked == true)
{
p = "1";
}
else
{
p = "0";
}
textBox1.Text = a + b + c + d + ee + f + g + h + i + j + k + l + m + n + o + p;
}感觉20行代码能完成的功能,我用了200行!! 请高手把这段代码变短或用更好的方法实现!!感激不尽!!!
for (int i=0; i<16; i++)
{
if (checkBox[i].Checked)
{
n += Math.Pow(2, i);
}
}再把n转成2进制字符串就可以了
System.Text.StringBuilder sb = new System.Text.StringBuilder();
for(int i = 1; i < = 16; i++)
{
CheckBox cb = this.Controls["CheckBox" + i] as CheckBox;
if(cb != null)
sb.AppendFormat("{0}", cb.Checked ? "1" : "0");
}
textBox1.Text = sb.ToString();
for(int i=1;i<=16;i++)
{
//注意你控件名CheckBox的大小写
FieldInfo fi=this.GetType().GetField("CheckBox"+i,BindingFlags.NonPublic|BindingFlags.Public|BingdingFlags.Instance);
CheckBox cb=fi.GetValue(this) as CheckBox;
sb.Append(cb.Checked?"1":"0");
}
TextBox1.Text=sb.ToString();
GetField("checkBox"+i,
private void button1_Click(object sender, EventArgs e)
{
StringBuilder s = new StringBuilder(16);
for (int i = 0; i < checkedListBox1.Items.Count; i++)
{
s.Append((checkedListBox1.GetItemChecked(i) == true) ? "1" : "0");
}
textBox1.Text = s.ToString();
}
foreach (Control c in this.Controls)
{
if (c is System.Windows.Forms.CheckBox)
{
((CheckBox)c).CheckedChanged += new System.EventHandler(changValues);
}
}
在类中加入下面方法: private void changValues(object sender, EventArgs e)
{
foreach (Control c in this.Controls)
{
if (c is System.Windows.Forms.CheckBox)
{
if (((CheckBox)c).Checked)
{
this.textBox1.Text = this.textBox1.Text + Convert.ToString(1);
}
else
{
this.textBox1.Text = this.textBox1.Text + Convert.ToString(0);
}
}
}
}
private void checkBox1_CheckedChanged(object sender, EventArgs e)
{
CheckBox check = sender as CheckBox;
int index=flowLayoutPanel1.Controls.IndexOf(check);
check.Tag = check.Checked ? 1 : 0;
if (index == 0)
textBox1.Text = check.Tag.ToString() + textBox1.Text.Substring(1);
else if (index == 15)
textBox1.Text = textBox1.Text.Substring(0, index - 1) + check.Tag.ToString();
else
textBox1.Text = textBox1.Text.Substring(0, index) + check.Tag.ToString() + textBox1.Text.Substring(index + 1);
}
private void changValues(object sender, EventArgs e)
{
this.textBox1.Text = "";
foreach (Control c in this.Controls)
{
if (c is System.Windows.Forms.CheckBox)
{
if (((CheckBox)c).Checked)
{
this.textBox1.Text = Convert.ToString(1)+ this.textBox1.Text ;
}
else
{
this.textBox1.Text = Convert.ToString(0)+ this.textBox1.Text;
}
}
}
}
支持3楼的这种方法.对本题,还可简化 不用测试空,"{0}"也可不用StringBuilder sb = new StringBuilder();
for(int i = 1; i < = 16; i++)
{
CheckBox cb = this.Controls["CheckBox" + i] as CheckBox;
sb.AppendFormat(cb.Checked ? "1" : "0");
}
textBox1.Text = sb.ToString();