Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3783236
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:11:45+00:00 2026-05-19T11:11:45+00:00

I have a ListBox named lsbEntities . I want to filter it’s items based

  • 0

I have a ListBox named lsbEntities. I want to filter it’s items based on some selected radio button.

The code below is kind of pseudo, is their a better approach?

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;
         }
     }
 }

I have only included an example of selecting to filter everything but VehicleS. The same approach would be used for the Facility class, where the thread would be re-instantiated.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T11:11:46+00:00Added an answer on May 19, 2026 at 11:11 am

    Here is a simple example showing one way to filter items in a ListBox. This could be improved by using a ListView or DataGridView in VirtualMode. It is very unclear to me what you are trying to do, so if this is not helpful I will remove it.

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ListBox which displays items of variable height. I want to show
In the example below I have a ListBox with dozens of font names in
I have the following code: ListBox.DataSource = DataSet.Tables(table_name).Select(some_criteria = match) ListBox.DisplayMember = name The
I have a listbox where the items contain checkboxes: <ListBox Style={StaticResource CheckBoxListStyle} Name=EditListBox> <ListBox.ItemTemplate>
I have a listbox in a form, and based on the answers within I
I have a listbox in a Silverlight C# app which is binded to some
I have a listbox named lbselectedcom in my web page. and i am binding
I have 2 user controls one named Filters and one named FilterItem Filter looks
I have a horizontal ListBox of items, each of which has two properties: Name
I have a dropdown listbox on my main page with a button on the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.