2011-03-17 54 views
0

我能夠實現自動完成文本框搜索,但其區分大小寫。我想讓它變得不敏感。我已經提出或條件,但它只檢查輸入的第一個字母。我希望搜索完全不區分大小寫。Dotnet:如何使自動完成文本框搜索不區分大小寫?

下面是我的代碼

public partial class Form1 : Form 
{ 
    AutoCompleteStringCollection acsc; 
    public Form1() 
    { 
     InitializeComponent(); 
     acsc = new AutoCompleteStringCollection(); 
     textBox1.AutoCompleteCustomSource = acsc; 
     textBox1.AutoCompleteMode = AutoCompleteMode.None; 
     textBox1.AutoCompleteSource = AutoCompleteSource.CustomSource; 
     acsc.Add("Sim Vodafone"); 
     acsc.Add("sim vodafone"); 
     acsc.Add("sIm"); 
     acsc.Add("siM"); 
    } 

    private void textBox1_TextChanged(object sender, EventArgs e) 
    { 
     string d = null; 

     listBox1.Items.Clear(); 
     if (textBox1.Text.Length == 0) 
     { 
      hideResults(); 
      return; 
     } 
     foreach (String s in textBox1.AutoCompleteCustomSource) 
     { 
      d = textBox1.Text.ToUpper(); 
      if (s.Contains(d) || s.Contains(textBox1.Text)) 
      { 
       Console.WriteLine("Found text in: " + s); 
       listBox1.Items.Add(s); 
       listBox1.Visible = true; 
      } 
     } 
    } 

    private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 
    { 
     textBox1.Text = listBox1.Items[listBox1.SelectedIndex].ToString(); 
     hideResults(); 
    } 

      void listBox1_LostFocus(object sender, System.EventArgs e) 
    { 
     hideResults(); 
    } 

    void hideResults() 
    { 
     listBox1.Visible = false; 
    } 
} 

}

回答

2

我想唯一的事情,唯一缺少的字符串轉換在您autoCompleteSource到上層。更改

d = textBox1.Text.ToUpper(); 
if (s.Contains(d) || s.Contains(textBox1.Text)) 
{ 
    Console.WriteLine("Found text in: " + s); 
    listBox1.Items.Add(s); 
    listBox1.Visible = true; 
} 

d = textBox1.Text.ToUpper(); 
string upperS = s.ToUpper(); 
if (upperS.Contains(d)) 
{ 
    Console.WriteLine("Found text in: " + s); 
    listBox1.Items.Add(s); 
    listBox1.Visible = true; 
} 

,它應該工作。雖然我確信,應該有一個簡單的自動完成解決方案,而不是創建自己的列表框。

+0

感謝您的回答。它非常簡單。我創建了一個ListBox – Prachur 2011-03-17 07:43:22

0

你可以試試這個。

d = textBox1.Text; 
    if (s.Contains(d.ToUpper()) || s.Contains(d.ToLower()) || s.Contains(textBox1.Text.ToUpper()) || Contains(textBox1.Text.ToLower()))