简体   繁体   中英

C# Update Label depending on which item in the Combobox is chosen

I have tried to change a label in my Form depending on what I choose in the combobox. It does not work could somebody help.

Here is my code:

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

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

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

Use || instead... index can't be 1 AND 2 at the same time.. It could also be that you don't selection 1 is kg, and 2 is grams.. if that's the case, yoy could use a switch:

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

  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM