1

我试图根据我在组合框中选择的内容更改表单中的标签。它不起作用,有人可以帮忙。

这是我的代码:

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

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

    }
4

1 回答 1

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

使用 || 相反...索引不能同时为12..也可能是您没有选择1是公斤,2是克..如果是这种情况,您可以使用开关:

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

  }
于 2012-12-01T18:56:25.093 回答