自动完成TextBox控件

我想有一个文本框控件,用C#2008和LINQ在Windows应用程序中建议和附加数据库中的值。

我是用combobox做的,但我不能用文本框来做。

我该怎么做?

这可能不是最好的做事方式,但应该有效:

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; } } } 

查看AutoCompleteSourceAutoCompleteCustomSourceAutoCompleteMode属性。

 textBox1.AutoCompleteMode = AutoCompleteMode.Suggest; textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; AutoCompleteStringCollection col = new AutoCompleteStringCollection(); col.Add("Foo"); col.Add("Bar"); textBox1.AutoCompleteCustomSource = col; 

请注意,设计师允许您在不编写任何代码的情况下执行此操作…

当然这取决于你如何实现它,但也许这是一个好的开始:

 using System.Windows.Forms; public class AutoCompleteTextBox : TextBox { private string[] database;//put here the strings of the candidates of autocomplete private bool changingText = false; protected override void OnTextChanged (EventArgs e) { if(!changingText && database != null) { //searching the first candidate string typed = this.Text.Substring(0,this.SelectionStart); string candidate = null; for(int i = 0; i < database.Length; i++) if(database[i].Substring(0,this.SelectionStart) == typed) { candidate = database[i].Substring(this.SelectionStart,database[i].Length); break; } if(candidate != null) { changingText = true; this.Text = typed+candidate; this.SelectionStart = typed.Length; this.SelectionLength = candidate.Length; } } else if(changingText) changingText = false; base.OnTextChanged(e); } } 

我不确定这是否运行良好,但我认为这段代码的基础已经足够好了。

 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); } } 

要达到这个结果:

在此处输入图像描述

您可以按照两种方式,第一种是环境属性选项卡,并设置以下属性:

在此处输入图像描述

最好的方法是通过代码创建这个效果,请参阅我的示例如下:

 AutoCompleteStringCollection sourceName = new AutoCompleteStringCollection(); foreach (string name in listNames) { sourceName.Add(name); } txtName.AutoCompleteCustomSource = sourceName; txtName.AutoCompleteMode = AutoCompleteMode.Suggest; txtName.AutoCompleteSource = AutoCompleteSource.CustomSource; 
  private void TurnOnAutocomplete() { textBox.AutoCompleteMode = AutoCompleteMode.SuggestAppend; textBox.AutoCompleteSource = AutoCompleteSource.CustomSource; AutoCompleteStringCollection collection = new AutoCompleteStringCollection(); string[] arrayOfWowrds = new string[]; try { //Read in data Autocomplete list to a string[] string[] arrayOfWowrds = new string[]; } catch (Exception err) { MessageBox.Show(err.Message, "File Missing", MessageBoxButtons.OK, MessageBoxIcon.Error); } collection.AddRange(arrayOFWords); textBox.AutoCompleteCustomSource = collection; } 

在您拥有自动完成列表所需的数据后,您只需要调用一次。 绑定后,它将保留在textBox中。 每次在textBox中更改文本时,您都不需要或想要调用它,这将导致程序中断。

您可以附加到KeyDown事件,然后在数据库中查询用户已输入的文本部分。 例如,如果用户输入“T”,则搜索以“T”开头的内容。 然后,当他们输入下一个字母,例如“e”时,搜索表格中以“Te”开头的内容。

例如,可用项目可以显示在“浮动”ListBox中。 您需要将ListBox放在TextBox下面,以便他们可以看到可用的条目,然后在完成键入时删除ListBox。

 private void textBox1_TextChanged(object sender, EventArgs e) { try { textBox1.AutoCompleteMode = AutoCompleteMode.Suggest; textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; AutoCompleteStringCollection col = new AutoCompleteStringCollection(); con.Open(); sql = "select *from Table_Name; cmd = new SqlCommand(sql, con); SqlDataReader sdr = null; sdr = cmd.ExecuteReader(); while (sdr.Read()) { col.Add(sdr["Column_Name"].ToString()); } sdr.Close(); textBox1.AutoCompleteCustomSource = col; con.Close(); } catch { } } 
  You can add a parameter in the query like @emailadd to be added in the aspx.cs file where the Stored Procedure is called with cmd.Parameter.AddWithValue. The trick is that the @emailadd parameter doesn't exist in the table design of the select query, but being added and inserted in the table. USE [DRDOULATINSTITUTE] GO /****** Object: StoredProcedure [dbo].[ReikiInsertRow] Script Date: 5/18/2016 11:12:33 AM ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER procedure [dbo].[ReikiInsertRow] @Reiki varchar(100), @emailadd varchar(50) as insert into dbo.ReikiPowerDisplay select Reiki,ReikiDescription, @emailadd from ReikiPower where Reiki=@Reiki; Posted By: Aneel Goplani. CIS. 2002. USA