2017-07-31 67 views
-1

我添加了一个复选框,并在那里调用方法名称Test。 随着测试模式我生成随机字符串,并将它们添加到listView作为项目。并且将这些项目保存在列表listv中。如何在ListView中搜索后在ListView中过滤项目?

List<string> listv; 
    private void Test() 
    { 
     if (checkBox1.Checked) 
     { 
      for (int i = 0; i < random.Next(20,1000); i++) 
      { 
       string rand = RandomString(200); 
       ListViewCostumControl.lvnf.Items.Add(rand); 
      } 
      textBox4.Enabled = true; 

      listv = ListViewCostumControl.lvnf.Items.Cast<ListViewItem>() 
          .Select(item => item.Text) 
          .ToList(); 
     } 
     else 
     { 
      ListViewCostumControl.lvnf.Items.Clear(); 
      textBox4.Enabled = false; 
     } 
    } 

    private static Random random = new Random(); 
    public static string RandomString(int length) 
    { 
     const string chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
     return new string(Enumerable.Repeat(chars, length) 
      .Select(s => s[random.Next(s.Length)]).ToArray()); 
    } 

    private void checkBox1_CheckedChanged(object sender, EventArgs e) 
    { 
     Test(); 
    } 

然后在textBox4_TextChanged事件列表视图我在ListView中搜索特定的文本生成随机的物品后:

private void textBox4_TextChanged(object sender, EventArgs e) 
     {    
      if (textBox4.Text != "") 
      { 
       int itemsfound = 0; 
       ListViewCostumControl.lvnf.Items.Clear(); 
       for (int i = 0; i < listv.Count; i++) 
       { 
        if (listv[i].ToLower().Contains(textBox4.Text.ToLower())) 
        { 
         itemsfound++; 
         ListViewCostumControl.lvnf.Items.Add(listv[i]); 
         ListViewCostumControl.lvnf.Items[i].ForeColor = Color.Red; 
         backgroundWorker2.ReportProgress(0, itemsfound); 
        } 
       } 
      } 
      else 
      { 
       ListViewCostumControl.lvnf.Items.Clear(); 
       for (int i = 0; i < listv.Count; i++) 
       { 
        ListViewCostumControl.lvnf.Items.Add(listv[i]); 
        ListViewCostumControl.lvnf.Items[i].ForeColor = Color.Black; 
       } 
        backgroundWorker2.ReportProgress(0, "0"); 
      } 
     } 

我过滤的搜索,这样它只会显示找到的项目。 问题是我无法为找到的项目着色。 我想:

itemsfound++; 
ListViewCostumControl.lvnf.Items.Add(listv[i]); 
ListViewCostumControl.lvnf.Items[i].ForeColor = Color.Red; 
backgroundWorker2.ReportProgress(0, itemsfound); 

但是就行了越来越异常:

ListViewCostumControl.lvnf.Items[i].ForeColor = Color.Red; 

如果我键入textBox4一个字母,将颜色为红色的所有项目,并会显示在一个数量第一次找到的项目的标签。 下一次,当我键入另一个字母(不删除第一个,但又添加了另一个字母),那么我得到的例外:

InvalidArgument ='5'的值不适用于'索引'。

我循环listv所以在这种情况下变量'我'是5,但在listView我只有2项。

lvnf是ListView和它有2个项目,但“我”是5

如何解决它的价值?

我想要做的是过滤结果,它工作正常,直到我尝试着色过滤的发现项目。我想用红色对每个找到的项目进行着色。

之前,我想它颜色红色代码是:

ListViewCostumControl.lvnf.Items.Clear(); 
for (int i = 0; i < listv.Count; i++) 
    { 
     if (listv[i].ToLower().Contains(textBox4.Text.ToLower())) 
      ListViewCostumControl.lvnf.Items.Add(listv[i]); 
    } 

但现在我要的颜色找到的项目。

回答

0

的问题是,你遍历listv但只有当一个项目被发现增加lvnf。所以你现在不能使用i作为指标来设置颜色,为指标现在不同步。

你需要找到你想要使用目前的元素listv

lvnf目标尝试是这样的元素:

ListViewCostumControl.lvnf.Items.Single(x => x.Text == listv[i].Text).ForeColor = Color.Red; 

就守则草案,可能会需要一些调整..但希望你明白了!

+0

我得到错误的查找:没有任何参数给出对应于'ListView.ListViewItemCollection.Find(字符串,布尔)'所需的形式参数'searchAllSubItems'' –

+0

也循环循环工作正常。 –

+0

查看我更新的代码.. 此外,您的循环无法正常工作,或者您不会尝试访问不存在的索引 – DNKROZ