110,538
社区成员
发帖
与我相关
我的任务
分享
int count;
private void button1_Click(object sender, EventArgs e)
{
count = 0;
foreach (Control ctrl in panel1.Controls)
{
if (ctrl is ComboBox)
{
ComboBox cmb = ctrl as ComboBox;
count = Convert.ToInt32(cmb.SelectedItem) + count;
}
}
MessageBox.Show(count.ToString());
}
int count;
private void button1_Click(object sender, EventArgs e)
{
count = 0;
foreach (Control ctrl in panel1.Controls)
{
if (ctrl is ComboBox)
{
ComboBox cmb = ctrl as ComboBox;
count = Convert.ToInt32(cmb.SelectedItem) + count;
}
}
MessageBox.Show(count.ToString());
}
想用循环做。。因为后面还会有更多的combobox。。 直接后台取就行了啊
直接后台取就行了啊
先将为空、不为数字的算做0,然后相加得出结果private void button1_Click(object sender, EventArgs e) { double sum = string.IsNullOrEmpty(comboBox1.Text.Trim()) && !double.TryParse(comboBox1.Text.Trim(), out ios) ? 0 : Convert.ToDouble(comboBox1.Text)+ string.IsNullOrEmpty(comboBox2.Text.Trim()) && !double.TryParse(comboBox2.Text.Trim(), out ios) ? 0 : Convert.ToDouble(comboBox2.Text)+ string.IsNullOrEmpty(comboBox3.Text.Trim()) && !double.TryParse(comboBox3.Text.Trim(), out ios) ? 0 : Convert.ToDouble(comboBox3.Text)+ string.IsNullOrEmpty(comboBox4.Text.Trim()) && !double.TryParse(comboBox4.Text.Trim(), out ios) ? 0 : Convert.ToDouble(comboBox4.Text)+ string.IsNullOrEmpty(comboBox5.Text.Trim()) && !double.TryParse(comboBox5.Text.Trim(), out ios) ? 0 : Convert.ToDouble(comboBox5.Text); }
private void button1_Click(object sender, EventArgs e) { int count = Convert.ToInt32(comboBox1.SelectedItem) + Convert.ToInt32(comboBox2.SelectedItem) +Convert.ToInt32(comboBox3.SelectedItem)+Convert.ToInt32(comboBox4.SelectedItem) +Convert.ToInt32(comboBox5.SelectedItem); }
private void button1_Click(object sender, EventArgs e)
{
double sum =
string.IsNullOrEmpty(comboBox1.Text.Trim()) && !double.TryParse(comboBox1.Text.Trim(), out ios) ? 0 : Convert.ToDouble(comboBox1.Text)+
string.IsNullOrEmpty(comboBox2.Text.Trim()) && !double.TryParse(comboBox2.Text.Trim(), out ios) ? 0 : Convert.ToDouble(comboBox2.Text)+
string.IsNullOrEmpty(comboBox3.Text.Trim()) && !double.TryParse(comboBox3.Text.Trim(), out ios) ? 0 : Convert.ToDouble(comboBox3.Text)+
string.IsNullOrEmpty(comboBox4.Text.Trim()) && !double.TryParse(comboBox4.Text.Trim(), out ios) ? 0 : Convert.ToDouble(comboBox4.Text)+
string.IsNullOrEmpty(comboBox5.Text.Trim()) && !double.TryParse(comboBox5.Text.Trim(), out ios) ? 0 : Convert.ToDouble(comboBox5.Text);
}
private void button1_Click(object sender, EventArgs e)
{
int count = Convert.ToInt32(comboBox1.SelectedItem) + Convert.ToInt32(comboBox2.SelectedItem)
+Convert.ToInt32(comboBox3.SelectedItem)+Convert.ToInt32(comboBox4.SelectedItem)
+Convert.ToInt32(comboBox5.SelectedItem);
}