使用Leave事件禁用Combobox正在选择文本

时间:2012-08-09 15:22:33

标签: c# winforms combobox

我在Combobox上使用Leave事件来禁用它并加载一些数据,但在离开和禁用后,它会选择Combobox中的整个文本。我想取消选择该文本。

我正在使用:

cmbNome.DropDownStyle = ComboBoxStyle.DropDown;    
cmbNome.AutoCompleteMode = AutoCompleteMode.Suggest;    
cmbNome.AutoCompleteSource = AutoCompleteSource.CustomSource;

如果它有帮助......

代码:

    private void cmbNome_Leave(object sender, EventArgs e)
    {
        cmbNome.Enabled = false;
        CarregarDados();
        CarregarTelefones();
    }

2 个答案:

答案 0 :(得分:0)

在禁用组合框之前尝试设置数据源:

        string[] data = new string[] {
            "Absecon","Abstracta","Abundantia","Academia","Acadiau","Acamas",
            "Ackerman","Ackley","Ackworth","Acomita","Aconcagua","Acton","Acushnet",
            "Acworth","Ada","Ada","Adair","Adairs","Adair","Adak","Adalberta","Adamkrafft",
            "Adams"

        };


        private void comboBox2_Leave(object sender, EventArgs e)
        {
            comboBox2.DataSource = data;
            comboBox2.Enabled = false;
            comboBox2.SelectedIndex = -1;
        }

此代码用数据填充组合;但是,没有选择任何项目,文本字段为空

编辑:添加样本数据

答案 1 :(得分:0)

尝试首先取消选择项目,然后禁用组合框。

cmbNome.SelectedIndex = -1;
cmbNome.Enabled = false;