WinForms ComboBox DropDown and Autocomplete window both appear

后端 未结 10 1307
礼貌的吻别
礼貌的吻别 2020-12-05 17:41

I\'ve got a ComboBox on a winforms app with this code:

comboBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
comboBox1.AutoCompleteSource         


        
10条回答
  •  醉酒成梦
    2020-12-05 18:15

    WinForms ComboBox DropDown...the answer is this...
    write below code in comboBox1 Enter event..

    private void comboBox1_Enter(object sender, EventArgs e)
    {
        comboBox1.DroppedDown = true;
    }
    

    Now for comboBox1 AutoComplete...
    write this AutoComplete() in page load event..so it work...

    public void AutoComplete()
    {
        try
        {
            MySqlConnection conn = new 
            MySqlConnection("server=localhost;database=databasename;user
                id=root;password=;charset=utf8;");
            MySqlCommand cmd = new MySqlCommand("select distinct
                (columnName) from tablename", conn);
            DataSet ds = new DataSet();
            MySqlDataAdapter da = new MySqlDataAdapter(cmd);
            da.Fill(ds, "tablename");
            AutoCompleteStringCollection col = new
            AutoCompleteStringCollection();
    
            int i = 0;
            for (i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
            {
                col.Add(ds.Tables[0].Rows[i]["columnName"].ToString());
            }
            comboBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
            comboBox1.AutoCompleteCustomSource = col;
            comboBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
            if (conn.State == ConnectionState.Open)
            {
                conn.Close();
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
        MessageBoxIcon.Error);
        }
    }
    

提交回复
热议问题