简体   繁体   中英

c# listen for combobox selection change

background

I have ac# application written that interacts with an SQL database. a recent feature request was to allow for different types of authentication (3 types). I originally decided to use 3 radio buttons (one for each auth option) for this and 2 textboxes (username/password). this worked fine and all the background code works fine but they have now requested that when they use SSPI auth (requires no extra input from user) that i gray out the textboxes so information cannot be entered and when one of the other two options is chosen, to allow the boxes to be editable again. To make this cleaner i now have a single combobox with 3 items (auth) and 2 textboxes (un/pw).

question

how do i have the application listen for changes to the combobox before the user clicks run? I have always used a button as a catalyst event and have not had to do this before. I have seen a few examples where i can use the condition (if selected index is equal to x) do blah, but it seems to require my start button to be pressed anyways and not a workable solution. i have also found this example C# -comboBox Selected IndexChange that i do not quite understand and i believe requires a 2 boxes to be made but i dont know why.

sudo code

if ((combobox item is not selected) or (combobox selection == indexitem1))
{ 
    //then keep textboxes read only
}
else 
{
    //change textbox to editable
}

request

i need this listener to be able to tell when the combobox selection is any of the 3 choices and moves between any of them and will correctly change the textboxes to reflect the current selection regardless of previous state to current state.

Any help is greatly appreciated. Links, code, comments, questions. everything helps me see something i havent yet or helps me search for better answers. Thanks!

solution

i just figured this out

private void AuthSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            //listen if combobox selection is changed
            if ((AuthSelect.SelectedIndex == 0) || (AuthSelect.SelectedIndex == -1))
            {
                userName.ReadOnly = true;
                password.ReadOnly = true;
            }
            else
            {
                userName.ReadOnly = false;
                password.ReadOnly = false;
            }      
        }
private void AuthSelect_SelectedIndexChanged(object sender, EventArgs e)
        {
            //listen if combobox selection is changed
            if ((AuthSelect.SelectedIndex == 0) || (AuthSelect.SelectedIndex == -1))
            {
                userName.ReadOnly = true;
                password.ReadOnly = true;
            }
            else
            {
                userName.ReadOnly = false;
                password.ReadOnly = 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