簡體   English   中英

Dotnet: - 如何使自動填充文本框搜索不區分大小寫?

[英]Dotnet :- How to make Autocomplete textbox search case insensitive?

我能夠實現自動完成文本框搜索,但它區分大小寫。 我想讓它變得麻木不仁。 我已經放了一個或者條件,但它只檢查第一個輸入的字母。 我希望搜索完全不區分大小寫。

以下是我的代碼

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

}

我認為唯一的事情就是將autoCompleteSource中的字符串轉換為upper。 更改

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

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

你能試試嗎?

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM