AutoComplete TextBox Control

前端 未结 9 1283
误落风尘
误落风尘 2020-11-27 04:48

I want to have a textbox control that suggests and append values from a database in a Windows application with C# 2008 and LINQ.

I do it with a combobox but I can\'t

9条回答
  •  日久生厌
    2020-11-27 05:55

    To AutoComplete TextBox Control in C#.net windows application using 
    wamp mysql database...
    
    here is my code..
    
    AutoComplete();
    
    write this **AutoComplete();** text in form-load event..
    
    private void Autocomplete()
        {
            try
            {
                MySqlConnection cn = new MySqlConnection("server=localhost;
    database=databasename;user id=root;password=;charset=utf8;");
                cn.Open();
                MySqlCommand cmd = new MySqlCommand("SELECT distinct Column_Name
         FROM table_Name", cn);
                DataSet ds = new DataSet();
                MySqlDataAdapter da = new MySqlDataAdapter(cmd);
                da.Fill(ds, "table_Name");
                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]["Column_Name"].ToString());
    
                }
                textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBox1.AutoCompleteCustomSource = col;
                textBox1.AutoCompleteMode = AutoCompleteMode.Suggest;
                cn.Close();
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK,
           MessageBoxIcon.Error);
            }
        }
    

提交回复
热议问题