2017-07-31 66 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