AutoComplete TextBox Control

前端 未结 9 1232
误落风尘
误落风尘 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:52

    There are two ways to accomplish this textbox effect:

    Either using the graphic user interface (GUI); or with code

    Using the Graphic User Interface:
    Go to: "Properties" Tab; then set the following properties:

    However; the best way is to create this by code. See example below.

    AutoCompleteStringCollection sourceName = new AutoCompleteStringCollection();
    
    foreach (string name in listNames)
    {    
        sourceName.Add(name);
    }
    
    txtName.AutoCompleteCustomSource = sourceName;
    txtName.AutoCompleteMode = AutoCompleteMode.Suggest;
    txtName.AutoCompleteSource = AutoCompleteSource.CustomSource;
    
    0 讨论(0)
  • 2020-11-27 05:55

    This might not be the best way to do things, but should work:

     this.textBox1.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
     this.textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource;
    
    private void textBox1_TextChanged(object sender, EventArgs e)
    {
        TextBox t = sender as TextBox;
        if (t != null)
        {
            //say you want to do a search when user types 3 or more chars
            if (t.Text.Length >= 3)
            {
                //SuggestStrings will have the logic to return array of strings either from cache/db
                string[] arr = SuggestStrings(t.Text);
    
                AutoCompleteStringCollection collection = new AutoCompleteStringCollection();
                collection.AddRange(arr);
    
                this.textBox1.AutoCompleteCustomSource = collection;
            }
        }
    }
    
    0 讨论(0)
  • 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);
            }
        }
    
    0 讨论(0)
提交回复
热议问题