简体   繁体   中英

C#: if statement error when using the combobox selection

When selecting 'Jewelry' from the combobox the "txt_qty & label6" is viewing which is in the form but when selecting Gem the "textBox5 & label18" its not viewing. please help

 private void combo_main_type_SelectedIndexChanged(object sender, EventArgs e)
    {

        if ((string)combo_main_type.SelectedItem == "Jewelry")
        {
            txt_qty.Visible = true;
            label6.Visible = true;
            txt_qty.Location = new System.Drawing.Point(213, 343);
            label6.Location = new System.Drawing.Point(40, 348);
        }
        else if ((string)combo_main_type.SelectedItem == "")
        {
            txt_qty.Visible = false;
            label6.Visible = false;

        }
        else if ((string)combo_main_type.SelectedItem == "Gem")
        {
            textBox5.Visible = true;
            label18.Visible = true;
            textBox5.Location = new System.Drawing.Point(213, 343);
            label18.Location = new System.Drawing.Point(40, 348);
        }
        else
        {
            textBox5.Visible = false;
            label18.Visible = false;
        }


    }

What you need to do is following.

private void combo_main_type_SelectedIndexChanged(object sender, EventArgs e)
{

    if ((string)combo_main_type.SelectedItem == "Jewelry")
    {
        txt_qty.Visible = true;
        label6.Visible = true;
        txt_qty.Location = new System.Drawing.Point(213, 343);
        label6.Location = new System.Drawing.Point(40, 348);
        textBox5.Visible = false;
        label18.Visible = false;
    }
    else if ((string)combo_main_type.SelectedItem == "Gem")
    {
        textBox5.Visible = true;
        label18.Visible = true;
        textBox5.Location = new System.Drawing.Point(213, 343);
        label18.Location = new System.Drawing.Point(40, 348);
        txt_qty.Visible = false;
        label6.Visible = false;
    }
    else
    {
        textBox5.Visible = false;
        label18.Visible = false;

        txt_qty.Visible = false;
        label6.Visible = false;
    }

}

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