2016-06-22 45 views
-1
private void textBox4_TextChanged(object sender, EventArgs e) 
     { 
      foreach (ListViewItem item in ListViewCostumControl.lvnf.Items) 
      { 
       if (item.Text.ToLower().StartsWith(textBox1.Text.ToLower())) 
       { 
        item.Selected = true; 
        item.BackColor = Color.CornflowerBlue; 
        item.ForeColor = Color.White; 
       } 
       else 
       { 
        item.Selected = false; 
        item.BackColor = Color.White; 
        item.ForeColor = Color.Black; 
       } 
      } 
      if (ListViewCostumControl.lvnf.SelectedItems.Count == 1) 
      { 
       ListViewCostumControl.lvnf.Focus(); 
      } 
     } 

例如在listView中,如果我有30個項目,並且我在文本框F中鍵入,所以它會向我顯示所有F項目,但是我繼續輸入Form1,那麼它應該自動縮小結果到Form1我的意思是在CornflowerBlue中使用它們或在Red中使用更好的顏色。如何使用textBox textchanged事件搜索listView特定項目?

因此,首先當我只鍵入F時,它會以紅色顯示許多項目,然後我繼續鍵入Form1,這樣它會着色更少的項目等等。

此代碼根本不着色。

+0

我只是想用你的代碼。它完美的作品。你有什麼錯誤嗎? – SilentCoder

+0

@codelahiru無論我在textBox form1或Form1或FORM1中輸入什麼,它都會將所有時間都設置爲黑白色,並且永遠不會添加到CornflowerBlue –

回答

0

TextChanged事件是textBox4,但你的代碼檢查textBox1文本。

我可以想象你想成爲檢查您正在打字的過濾器相同的文本框中的文本。

變化

if (item.Text.ToLower().StartsWith(textBox1.Text.ToLower())) 

if (item.Text.ToLower().StartsWith(textBox4.Text.ToLower())) 
+0

Ooops ...我的錯誤對不起。 –

+2

沒問題。這只是其中一個經驗教訓,每個人都知道他們現在開始正確地命名變量和控件,而不是'foo1','foo2','foo3'等。同樣,單個斷點和一些調試也會顯示出問題;) – TyCobb

0

你可以做的只是在0123部分中添加item.Remove()。然後它會縮小名單。 但是,您需要將這些已移除的項目保留在其他位置並進行適當添加。否則它將是一個空的列表。

item.Selected = false; 
item.BackColor = Color.White; 
item.ForeColor = Color.Black; 
item.Remove(); 

enter image description here

+0

不,我不想縮小listView中的項目列表,而是縮小要着色的項目數量。如果我只鍵入F,它會着色幾乎所有的項目,因爲字母F可能存在於許多項目文本中。但是,如果我將繼續輸入Form1,那麼它將縮小要着色的項目數量。不要縮小項目的數量。這個想法只是在項目中搜索不刪除它們。 –

0

我建的形式,並使用你的代碼(沒有 「ListViewCostumControl.lvnf」) ,它的運作良好。 這是代碼:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 
    private void Form1_Load(object sender, EventArgs e) 
    { 
     listView1.View = View.Details; 
     listView1.GridLines = true; 
     listView1.FullRowSelect = true; 

     //Add column header 
     listView1.Columns.Add("ProductName", 100); 
     listView1.Columns.Add("Price", 70); 
     listView1.Columns.Add("Quantity", 70); 

     //Add items in the listview 
     string[] arr = new string[4]; 
     ListViewItem itm; 

     //Add first item 
     arr[0] = "product_1"; 
     arr[1] = "100"; 
     arr[2] = "10"; 
     itm = new ListViewItem(arr); 
     listView1.Items.Add(itm); 

     //Add second item 
     arr[0] = "product_2"; 
     arr[1] = "200"; 
     arr[2] = "20"; 
     itm = new ListViewItem(arr); 
     listView1.Items.Add(itm); 
    } 


    private void textBox4_TextChanged(object sender, EventArgs e) 
    { 
     foreach (ListViewItem item in listView1.Items) 
     { 
      if (item.Text.ToLower().StartsWith(textBox1.Text.ToLower())) 
      { 
       item.Selected = true; 
       item.BackColor = Color.CornflowerBlue; 
       item.ForeColor = Color.White; 
      } 
      else 
      { 
       item.Selected = false; 
       item.BackColor = Color.White; 
       item.ForeColor = Color.Black; 
      } 
     } 
     if (listView1.SelectedItems.Count == 1) 
     { 
      listView1.Focus(); 
     } 
    } 
} 

所以或許probkem在 「ListViewCostumControl.lvnf」 ..

+0

不確定問題出在哪裏。在我的情況下,無論我在文本框中輸入什麼,如果它只有一個字母'F',或者它是Form1或FORM1或form1,或者只是在所有情況下形成,它都會轉到白色和黑色部分。並且永遠不會去CornflowerBlue顏色部分。 –

相關問題