使用文本框和按钮在列表框中搜索项目 [英] Searching for an item within a ListBox using a TextBox and a Button

查看:28
本文介绍了使用文本框和按钮在列表框中搜索项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个系统,其中包含用户插入的整数 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.");
        }

    }

这篇关于使用文本框和按钮在列表框中搜索项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆