2011-01-23 109 views
3

我有一個名爲lsbEntities的列表框。我想根據某些選定的單選按鈕過濾它的項目。過濾列表框

下面的代碼是一種僞,是他們更好的方法?

private List<string> _listBoxItemsToFilter; 
private Thread tFilterEntityList; 

enum EntityType 
{ 
    Vehicle, 
    Facility 
} 

private void FilterEntityList(EntityType entityType) 
{ 
    _listBoxItemsToFilter = new List<string>(); 
    Dictionary<string,string> entitiesAndClassTypes; 
    List<string> listBoxItems = new List<string>(); 

    for(int i = 0; i < lsbEntities.Count; i++) 
    { 
     //object listItem = lsbEntities.Items[i];   
     listBoxItems.Add(lsbEntities[i].ToString());  
    } 

    // get associated types 
    entityClassTypes = _controlFacade.GetClassTypes(listBoxItems); 

    foreach (KeyValuePair<string,string> 
      entityAndClass in entitiesAndClassTypes) 
    { 
     classType = entityAndClass.Value; 

     if(classType != entityType) 
     { 
       _listBoxItemsToFilter.Add(entityAndClass.Key);   
     } 
    } 

    RemoveFilterFromEntityListBox(); 
    AddFilterToEntityListBox(); 
} 

private void AddFilterToEntityListBox() 
{ 
    // DELEGATE NEEDED TO MODIFY LISTBOX FROM THREAD 
    foreach(string listBoxItem in _listBoxItemsToFilter) 
    { 
     if(lsbEntities.Contains(listBoxItem) 
     { 
      // REMOVE WITH DELEGATE 
     } 
    } 
} 

private void RemoveFilterFromEntityListBox() 
{ 
    // DELEGATE NEEDED TO MODIFY LISTBOX FROM THREAD 
    if(_listBoxItemsToFilter != null) 
    { 
     foreach(string listBoxItem in _listBoxItemsToFilter) 
     { 
      if(!lsbEntities.Contains(listBoxItem) 
      { 
      // REMOVE WITH DELEGATE 
      } 
     } 
    } 
} 

// EXAMPLE CALL WHEN CLICKING RADIO-BUTTON 
private void rbVehicles_CheckedChanged(object sender, EventArgs e) 
{ 
    switch (rbVehicles.Checked) 
    { 
     case (true): 
     { 
      object entityType = (object)EntityType.Vehicle; 
      tFilterEntityList = new Thread(FilterEntityList(entityType)); 
      tFilterEntityList.IsBackground = true; 
      tFilterEntityList.Start(); 
      //FilterEntityList(EntityType.Vehicle); 
      break; 
     } 
    } 
} 

我只包括選擇來過濾一切,但Vehicle S.同樣的方法將被用於Facility類,其中,所述螺紋會被重新實例化的一個例子。

回答

3

下面是一個簡單的例子,展示了一種過濾ListBox中項目的方法。這可以通過在VirtualMode中使用ListView或DataGridView來改進。我很不清楚你想要做什麼,所以如果這沒有幫助,我會刪除它。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Drawing; 
using System.Linq; 
using System.Windows.Forms; 

public class Form1 : Form 
{ 
    [STAThread] 
    static void Main() 
    { 
     Application.EnableVisualStyles(); 
     Application.SetCompatibleTextRenderingDefault(false); 
     Application.Run(new Form1()); 
    } 

    List<Entity> items = new List<Entity>() 
    { 
     new Entity(EntityType.Vehicle, "Car"), 
     new Entity(EntityType.Vehicle, "Aeroplane"), 
     new Entity(EntityType.Vehicle, "Truck"), 
     new Entity(EntityType.Vehicle, "Bus"), 
     new Entity(EntityType.Facility, "Garage"), 
     new Entity(EntityType.Facility, "House"), 
     new Entity(EntityType.Facility, "Shack"), 
    }; 

    ListBox listBox; 
    ComboBox comboBox; 

    public Form1() 
    { 
     Text = "Filtering Demo"; 
     ClientSize = new Size(500, 320); 
     Controls.Add(listBox = new ListBox 
     { 
      Location = new Point(10, 10), 
      Size = new Size(200, 300), 
     }); 
     Controls.Add(comboBox = new ComboBox 
     { 
      Location = new Point(230, 10), 
      DropDownStyle = ComboBoxStyle.DropDownList, 
      Items = { "All", EntityType.Vehicle, EntityType.Facility }, 
      SelectedIndex = 0, 
     }); 

     comboBox.SelectedIndexChanged += UpdateFilter; 
     UpdateFilter(comboBox, EventArgs.Empty); 
    } 

    void UpdateFilter(object sender, EventArgs e) 
    { 
     var filtered = items.Where((i) => comboBox.SelectedItem is string || (EntityType)comboBox.SelectedItem == i.EntityType); 
     listBox.DataSource = new BindingSource(filtered, ""); 
    } 
} 

enum EntityType { Vehicle, Facility, } 

class Entity : INotifyPropertyChanged 
{ 
    public string Name { get; private set; } 
    public EntityType EntityType { get; private set; } 
    public Entity(EntityType entityType, string name) { EntityType = entityType; Name = name; } 
    public override string ToString() { return Name ?? String.Empty; } 
    // Implementing INotifyPropertyChanged to eliminate (caught) BindingSource exceptions 
    public event PropertyChangedEventHandler PropertyChanged; 
} 
+0

我使用了BindingSource,因爲它簡化了代碼。我可以清除ListView的項目列表並使用已過濾的列表重新填充它。如何獲得未經過濾的列表與過濾無關,這正是我所想的。 – Tergiver 2011-01-23 03:31:01