C# 如何使筛选器不区分大小写?
我在为学校做一个ITP项目。在这个项目中,我这样做是为了当我向列表框中添加一个单词时,有一个过滤器在列表框中搜索该单词,如果匹配为false,则将该单词添加到列表中。但是这个过滤器不区分大小写,这意味着即使有奥迪,它也会添加单词audi,但是因为第一个字母是大写的,所以过滤器不会检测到这一点。此位的代码为C# 如何使筛选器不区分大小写?,c#,filter,listbox,uppercase,C#,Filter,Listbox,Uppercase,我在为学校做一个ITP项目。在这个项目中,我这样做是为了当我向列表框中添加一个单词时,有一个过滤器在列表框中搜索该单词,如果匹配为false,则将该单词添加到列表中。但是这个过滤器不区分大小写,这意味着即使有奥迪,它也会添加单词audi,但是因为第一个字母是大写的,所以过滤器不会检测到这一点。此位的代码为 private void btnAddWord_Click(object sender, EventArgs e) { if (this.lbxUnsortedList
private void btnAddWord_Click(object sender, EventArgs e)
{
if (this.lbxUnsortedList.Items.Contains(this.tbxAddWord.Text) == false)
{
//if the textbox is empty
if (tbxAddWord.Text == "")
{
MessageBox.Show("You have entered no value in the textbox.");
tbxAddWord.Focus();
}
//if the number of items in the listbox is greater than 29
if (lbxUnsortedList.Items.Count > 29)
{
MessageBox.Show("You have exceeded the maximum number of values in the list.");
tbxAddWord.Text = "";
}
//if the number of items in the listbox is less than 29
else
{
//add word to the listbox
this.lbxUnsortedList.Items.Add(this.tbxAddWord.Text);
//update tbxListBoxCount
tbxListboxCount.Text = lbxUnsortedList.Items.Count.ToString();
//onclick, conduct the bubble sort
bool swapped;
string temp;
do
{
swapped = false;
for (int i = 0; i < lbxUnsortedList.Items.Count - 1; i++)
{
int result = lbxUnsortedList.Items[i].ToString().CompareTo(lbxUnsortedList.Items[i + 1]);
if (result > 0)
{
temp = lbxUnsortedList.Items[i].ToString();
lbxUnsortedList.Items[i] = lbxUnsortedList.Items[i + 1];
lbxUnsortedList.Items[i + 1] = temp;
swapped = true;
}
}
} while (swapped == true);
tbxAddWord.Text = "";
}
}
if (this.lbxUnsortedList.Items.Contains(this.tbxAddWord.Text) == true)
{
MessageBox.Show("The word that you have added is already on the list");
tbxAddWord.Text = "";
tbxAddWord.Focus();
}
}
private void btnAddWord\u单击(对象发送方,事件参数e)
{
if(this.lbxUnsortedList.Items.Contains(this.tbxAddWord.Text)==false)
{
//如果文本框为空
如果(tbxAddWord.Text==“”)
{
Show(“您没有在文本框中输入任何值。”);
tbxAddWord.Focus();
}
//如果列表框中的项目数大于29
如果(lbxUnsortedList.Items.Count>29)
{
Show(“您已经超过了列表中的最大值。”);
tbxAddWord.Text=“”;
}
//如果列表框中的项目数小于29
其他的
{
//将单词添加到列表框中
this.lbxUnsortedList.Items.Add(this.tbxAddWord.Text);
//更新tbxListBoxCount
tbxListboxCount.Text=lbxUsertedList.Items.Count.ToString();
//onclick,进行冒泡排序
布尔交换;
字符串温度;
做
{
交换=假;
对于(int i=0;i0)
{
temp=lbxUnsortedList.Items[i].ToString();
lbxUnsortedList.Items[i]=lbxUnsortedList.Items[i+1];
lbxUnsortedList.Items[i+1]=温度;
交换=真;
}
}
}while(swapped==true);
tbxAddWord.Text=“”;
}
}
if(this.lbxUnsortedList.Items.Contains(this.tbxAddWord.Text)==true)
{
Show(“您添加的单词已经在列表中”);
tbxAddWord.Text=“”;
tbxAddWord.Focus();
}
}
我想知道如何使这个不区分大小写,这样即使第一个字母是大写,过滤器也会自动过滤。试试这个
public bool checkItemExist(string itemToCheck)
{
return lbxUnsortedList.Items.Cast<string>.Where(a=>a.ToLower().Equals(tbxAddWord.Text.ToLower()).Count > 0;
}
public bool checkItemExist(string itemToCheck)
{
返回lbxUsertedList.Items.Cast.Where(a=>a.ToLower().Equals(tbxAddWord.Text.ToLower()).Count>0;
}
我只是建议您使用这种情况,它不是最佳的,但它可以按照您想要的方式工作:
bool contains = false;
for (int i = 0; i < lbxUnsortedList.Items.Count; i++)
{
if (lbxUnsortedList.Items[i].ToString().ToLower() == this.tbxAddWord.Text.ToString().ToLower())
{
contains = true;
}
}
if (!contains)
{
//your code
}
else
{
MessageBox.Show("The word that you have added is already on the list");
tbxAddWord.Text = "";
tbxAddWord.Focus();
}
bool contains=false;
对于(int i=0;i
这应该会告诉你你在寻找什么
culture.CompareInfo.IndexOf(段落、单词、CompareOptions.IgnoreCase)
另外,您也可以尝试将单词存储为小写
this.lbxUnsortedList.Items.Add(this.tbxAddWord.Text.ToLower());
然后用同样的方法搜索字符串
this.lbxUsertedList.Items.Contains(this.tbxAddWord.Text.ToLower())==true
但如果您使用“lbxOnsortedList”进行显示,那么使用关键字为普通单词、值为小写的字典也可能会有所帮助
另外,如果您允许我抛出其他内容,请使用
string.IsNullOrEmpty(tbxAddWord.Text)
而不是tbxAddWord.Text==”
。此外,您可能需要使用tbxAddWord.Text=string.Empty;
在比较两个字符串值之前,必须将字符串转换为大写或小写
循环遍历列表框,lbxOnsortedList
,从列表中获取项目。并将每个字符串与文本框,tbxAddWord
中的输入字符串进行比较
for (int i = 0; i < lbxUnsortedList.Items.Count; i++)
{
if (lbxUnsortedList.Items[i].ToString().ToLower() == this.tbxAddWord.Text.ToString().ToLower())
{
//Your Code
}
}
for(int i=0;i
对我来说,它是这样工作的
var query=_repository.Get(e=>e.IsDeleteUser!=true,null,“角色”)
这段代码运行得很好。但是在错误消息弹出后,列表中会添加一个空格。有什么方法可以阻止它吗?呵呵,没问题。太好了,我可以帮忙。
if (!string.IsNullOrEmpty(filter.UserName))
{
query = query.Where(e => e.UserName.**ToLower()**.Contains(filter.UserName**.ToLower()**)).ToList();
}