【问题标题】:c# listen for combobox selection changec# 监听组合框选择变化
【发布时间】:2014-01-22 19:55:14
【问题描述】:

背景

我编写了一个与 SQL 数据库交互的 c# 应用程序。最近的一项功能请求是允许不同类型的身份验证(3 种类型)。我最初决定为此使用 3 个单选按钮(每个身份验证选项一个)和 2 个文本框(用户名/密码)。 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 , 以允许框再次可编辑。为了使这个更清洁,我现在有一个包含 3 个项目(auth)和 2 个文本框(un/pw)的组合框。

问题

如何让应用程序在用户单击运行之前侦听组合框的更改?我一直使用按钮作为催化剂事件,以前不必这样做。我已经看到了一些我可以使用条件(如果选择的索引等于 x)做 blah 的例子,但它似乎需要按下我的开始按钮,而不是一个可行的解决方案。我还发现了这个例子C# -comboBox Selected IndexChange,我不太明白,我认为需要制作 2 个盒子,但我不知道为什么。

sudo 代码

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

请求

我需要此侦听器能够判断组合框选择何时是 3 个选项中的任何一个,并在其中任何一个之间移动,并且将正确更改文本框以反映当前选择,而不管先前状态到当前状态。

非常感谢任何帮助。链接、代码、cmets、问题。一切都可以帮助我看到我还没有看到的东西,或者帮助我寻找更好的答案。谢谢!

解决方案

我刚刚想通了

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;
            }      
        }

【问题讨论】:

    标签: c# combobox textbox listener


    【解决方案1】:
    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;
                }      
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多