C# Dotnet:-如何使自动完成文本框搜索不区分大小写?
我能够实现自动完成文本框搜索,但它区分大小写。我想让它变得不敏感。我已经设置了一个or条件,但它只检查第一个输入的字母。我希望搜索完全不区分大小写 下面是我的代码C# Dotnet:-如何使自动完成文本框搜索不区分大小写?,c#,winforms,desktop-application,C#,Winforms,Desktop Application,我能够实现自动完成文本框搜索,但它区分大小写。我想让它变得不敏感。我已经设置了一个or条件,但它只检查第一个输入的字母。我希望搜索完全不区分大小写 下面是我的代码 public partial class Form1 : Form { AutoCompleteStringCollection acsc; public Form1() { InitializeComponent(); acsc = new AutoCompleteStrin
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;
}
}
}你能试试这个吗
d = textBox1.Text;
if (s.Contains(d.ToUpper()) || s.Contains(d.ToLower()) || s.Contains(textBox1.Text.ToUpper()) || Contains(textBox1.Text.ToLower()))
我认为唯一缺少的是将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;
}