使用文本框和按钮在列表框中搜索项目
我正在创建一个系统,其中包含用户插入的整数 ListBox
.我已经包含一个搜索按钮和一个搜索TextBox
,供用户在ListBox
中输入他们想要搜索的整数.一旦用户输入了整数,我希望显示一个消息框,通知用户有例如列表框中值为 '3' 的 1 个整数,或通知用户该整数不存在于列表框中的错误消息框.
I am creating a system that includes a ListBox
of integers inserted by the user. I have contained a search button and a search TextBox
for the user to input the integer they want to search for within the ListBox
. Once the user has inputted the integer, I want a message box to be displayed either informing the user that there is e.g. 1 integer of value '3' in the list box, or an error message box informing the user that the integer does not exist within the list box.
private void buttonSearch_Click(object sender, EventArgs e)
{
listBoxAddedIntegers.SelectedItems.Clear();
for (int i = listBoxAddedIntegers.Items.Count - 1;i>=0; i--) ;
{
if (listBoxAddedIntegers.Items[i].ToString().ToLower().Contains(textBoxSearch.Text.ToLower())) ;
{
listBoxAddedIntegers.SetSelected(i, true);
}
}
// ...
}
我不确定要包含在此处的代码,并且我已经插入的代码表明当前内容中不存在i".
I am not really sure on the code that I am meant to include here, and the code that I have already inserted suggests that 'i' does not exist in the current content.
有人可以帮忙吗?
private void buttonSearch_Click(object sender, EventArgs e)
{
listBoxAddedIntegers.SelectedItems.Clear();
var itemsFound = listBoxAddedIntegers.Items.Where(i=>i.ToString().ToLower().Contains(textBoxSearch.Text.ToLower())).ToList();
if(itemsFound == null)
{
MessageBox.Show("No matches found.");
}
else
{
MessageBox.Show("Found " + itemsFound.Count + " matches.");
}
}