简体   繁体   中英

dropdown list reset deafult value c#

I have a problem to reset a dropdownlist is c# I have hiden and showed dropdown's depending on a value in the first selection dropdown box my code did work but i remembered you can type in a combobox and changed it to dropdownlist in visual studio and after that nope not working anymore. so basically i don't want the items to be removed from list it must just be at the starting "empty value" like it was when program loaded here is the code that worked before the change

    if(serviceFault_cb.Text == "Report Fault")
        {
            serviceType_cb.Text = "";

            serviceType_cb.Hide();
            serviceType_lb.Hide();
            faultMain1_lb.Show();
            faultMain1_cb.Show();
        }
        else if (serviceFault_cb.Text == "Service and Faults")
        {

            serviceType_cb.Show();
            serviceType_lb.Show();
            faultMain1_lb.Show();
            faultMain1_cb.Show();

        }
        else
        {

            serviceType_cb.Show();
            serviceType_lb.Show();
            faultMain1_cb.Text = "";
            faultMain1_lb.Hide();
            faultMain1_cb.Hide();
        }

a basic if statement to hide and show combobox's just need the value to clear when box is hidden and loaded again

Add an empty item into each combobox that would work as a deselected value. Add it before you add the actual items, then you can deselect a value by doing

serviceType_cb.SelectedIndex = 0;
panel_erviceType.Show();

Also I'd suggest using a panel to encapsulate the combobox with it's corresponding label to hide them simultaneously.

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