试图寻找的ListView匹配字符串子项目 [英] Trying to search ListView for subitems matching a string

查看:143
本文介绍了试图寻找的ListView匹配字符串子项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我通过一个ListView有麻烦扫描以查找匹配的子项给定的字符串。这里是我的code:

I'm having trouble scanning through a ListView to locate a subitem matching a given string. Here's my code:

private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
    {
        string date = datePicker.Value.ToShortDateString();
        int count = Program.booker.listView.Items.Count;

        for (int i = 0; i < count; i++)
        {
            ListViewItem lvi = Program.booker.listView.Items[i];

            if (lvi.SubItems.Equals(date))
            {
                MessageBox.Show("Found!", "Alert");
                Program.booker.listView.MultiSelect = true;
                Program.booker.listView.Items[i].Selected = true;
            }
            else
            {
                MessageBox.Show("Nothing found for " + date, "Alert");
            }
        }
    }

ListView控件位于布克形式,而我从过滤器类访问它。我想搜索整个ListView的匹配我的日期字符串的任何项目。谢谢!

The ListView is located on the Booker form, and I'm accessing it from the Filter class. I'd like to search the entire ListView for any items matching my date string. Thanks!

推荐答案

您可以使用的 FindItemWithText 方法。

ListViewItem searchItem = null;
int index = 0;
do
{
    if (index < Program.booker.listView.Items.Count)
    {
        //true = search subitems
        //last false param = no partial matches (remove if you want partial matches)
        searchItem = Program.booker.listView.FindItemWithText(date, true, index, false);
        if (searchItem != null)
        {
            index = searchItem.Index + 1;

             //rest of code
        }
    }
    else
        searchItem =null;

} while (searchItem != null);

这篇关于试图寻找的ListView匹配字符串子项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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