2009-08-31 208 views
15

我想要一個文本框控件,用C#2008和LINQ在Windows應用程序的數據庫中建議和附加值。自動完成文本框控件

我用一個組合框來做,但我不能用文本框來做。

我該怎麼做?

+0

對不起,我沒有看到您使用的是Windows應用程序。 – sshow 2009-08-31 15:03:50

回答

32

這可能不是做事情的最好方法,但應該工作:

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

SuggestString不允許運行程序並且說「名稱SuggestStrings'在當前上下文中不存在「 – 2009-09-01 11:37:42

+13

@ mohammad-reza正如代碼中的註釋所示: // SuggestStrings將具有從cache/db返回字符串數組的邏輯。 您將不得不實施SuggestStrings。不要指望你會從SO複製代碼,它會開始工作。我們可以爲您提供指針。 – 2009-09-01 19:33:52

0

您可以附加到KeyDown事件,然後在數據庫中查詢用戶已經輸入的那部分文本。例如,如果用戶輸入「T」,搜索以「T」開頭的內容。然後,當他們輸入下一個字母時,例如「e」,搜索表格中以「Te」開頭的內容。

例如,可用項目可以顯示在「浮動」列表框中。您需要將ListBox放置在TextBox下方,以便他們可以看到可用的條目,然後在完成輸入時移除ListBox。

+0

編程的第一條規則:不要重新發明輪子;) – 2009-08-31 15:11:53

+0

是的。沒有意識到有這個選項。我不做數據綁定。 (開始使用.Net 1.0,試圖在使用某些數據綁定功能時發現它們,但發現它們限制太多。) – 2009-08-31 15:13:00

9

查看AutoCompleteSource,AutoCompleteCustomSourceAutoCompleteMode屬性。

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

注意,設計器允許你這樣做而無需編寫任何代碼...

+0

我想使用Database.Can幫助我嗎? 我想從數據庫中讀取建議和自動完成 – 2009-09-02 18:56:59

+1

@mohammad reza:您將不得不使用ADO.net編寫代碼來訪問數據庫。 – 2009-09-02 19:28:12

+0

只需填寫您的查詢結果列表 – 2009-09-02 20:07:10

1
當然

這取決於你如何實現它,但也許這是一個良好的開端:

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

} 

我不確定這是否工作得很好,但我認爲這段代碼的基礎是足夠好的。

+0

沒有意識到有這樣的內置屬性。我對System.Windows.Forms工作不夠,我使用它的最後一個版本是.Net Framework 1.1 – 2009-08-31 15:21:58

0
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 
     { 
     } 
    } 
1
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
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 [email protected]; 

Posted By: Aneel Goplani. CIS. 2002. USA 
0

爲了達到這樣的結果:

enter image description here

你可以遵循兩種方式S,由環境屬性選項卡中的冷杉和設置如下屬性:

enter image description here

最好的辦法是通過代碼產生這種效果,看我的例子爲如下:

AutoCompleteStringCollection sourceName = new AutoCompleteStringCollection(); 

foreach (string name in listNames) 
{  
    sourceName.Add(name); 
} 

txtName.AutoCompleteCustomSource = sourceName; 
txtName.AutoCompleteMode = AutoCompleteMode.Suggest; 
txtName.AutoCompleteSource = AutoCompleteSource.CustomSource; 
0
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; 
    } 

您只需在獲得自動完成列表所需的數據後再調用一次。綁定後,它保持與文本框。每當文本框中的文本發生更改時,您都不需要或不想調用它,這會殺死您的程序。