C#更新标签取决于选择Combobox中的项目

时间:2012-12-01 18:54:42

标签: c# combobox label

我试图根据我在组合框中选择的内容更改表单中的标签。 如果有人帮忙,它就行不通。

这是我的代码:

    private void cmbItems_SelectionChangeCommitted(object sender, EventArgs e)
    {
        int index = cmbItems.SelectedIndex;

        if (index == 1 && index == 2)
        {
            lblAmount.Text = "Weight in gram";
        }

    }

1 个答案:

答案 0 :(得分:2)

if (index == 1 && index == 2)
{
   lblAmount.Text = "Weight in gram";
}

使用||相反...索引不能同时为1 AND 2 .. 也可能是你没有选择1是kg,2是克..如果是这样的话,yoy可以使用开关:

 switch(index)
  {
     case 1:
        lblAmount.Text = "Weight in Kg"; break;
     case 2: 
       lblAmount.Text = "Weight in Grams";
       break;
     default: 
       lblAmount.Text = "Weight in Grams";

  }