如何通过选择comboBox值来禁用按钮

时间:2015-09-18 09:01:57

标签: c# winforms combobox

我有一个绑定到sql数据库的comoboBox,我在索引0添加了一个默认文本,就像这样

string s = "< -------------Select an application ----------->";
applicationComboBox.Items.Insert(0, s);
applicationComboBox.SelectedIndex = 0;

我想知道如果选择索引0处的字符串s,是否有办法禁用我的按钮?在我的comboBox中,我用while(SQLReader.Read()) method instead of using ValueMember和`DisplayMember

绑定了数据

这是我尝试但没有运气

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        for (int i = 1; i < applicationComboBox.Items.Count; i++)
        {
            string value = applicationComboBox.GetItemText(applicationComboBox.Items[0]);

            string s = "<------------- Select an application ----------->";

            if (value == s)
            {
                exportButton.Enabled = false;
                MessageBox.Show(value); //nothing happen
                this.teacherCheckListBox.DataSource = null;
                teacherCheckListBox.Items.Clear();
            }
            else
            {
                exportButton.Enabled = true;   
            }                  
        }
    }
}

1 个答案:

答案 0 :(得分:2)

使用SelectedIndex属性知道选择了哪个项目,如果是第一项,则禁用该按钮。

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    if (comboBox1.SelectedIndex == 0)
    {
        exportButton.Enabled = false;
    }
}