📜  c# listview filter contains - C# Code Example(1)

📅  最后修改于: 2023-12-03 14:59:40.427000             🧑  作者: Mango

C# ListView Filter Contains

ListView is a commonly used control in Windows Forms applications. It is used to display items in a list with columns. Sometimes, it can be challenging to locate specific items in a ListView control that contains a large number of items. Hence, you may need to filter the items in a ListView to show only the relevant data. This is where the filter function comes into play.

In this code example, I will show you how to filter a ListView using a 'Contains' function in C#. This will allow you to filter the items in a ListView to show only the ones that contain a particular string.

Step 1: Create a ListView Control

Firstly, you need to create a ListView control in your Windows Forms application. You can do this by dragging and dropping the ListView control from the Toolbox to your Windows Form.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // Create columns for the ListView
        listView1.Columns.Add("Name", 100);
        listView1.Columns.Add("Age", 50);
        listView1.Columns.Add("Country", 100);

        // Add items to the ListView
        listView1.Items.Add(new ListViewItem(new string[] { "John", "25", "USA" }));
        listView1.Items.Add(new ListViewItem(new string[] { "Sarah", "30", "Canada" }));
        listView1.Items.Add(new ListViewItem(new string[] { "David", "21", "Australia" }));
        listView1.Items.Add(new ListViewItem(new string[] { "Sophie", "27", "France" }));
        listView1.Items.Add(new ListViewItem(new string[] { "Lisa", "22", "UK" }));
    }
}

In this example, I have created a new ListView control and added some columns and items to it.

Step 2: Create a TextBox Control

Next, you need to create a TextBox control in your Windows Forms application. This control will be used to enter the text to filter the ListView.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // Create columns for the ListView
        listView1.Columns.Add("Name", 100);
        listView1.Columns.Add("Age", 50);
        listView1.Columns.Add("Country", 100);

        // Add items to the ListView
        listView1.Items.Add(new ListViewItem(new string[] { "John", "25", "USA" }));
        listView1.Items.Add(new ListViewItem(new string[] { "Sarah", "30", "Canada" }));
        listView1.Items.Add(new ListViewItem(new string[] { "David", "21", "Australia" }));
        listView1.Items.Add(new ListViewItem(new string[] { "Sophie", "27", "France" }));
        listView1.Items.Add(new ListViewItem(new string[] { "Lisa", "22", "UK" }));

        // Create a TextBox control
        TextBox tbFilter = new TextBox();
        tbFilter.Location = new Point(10, 10);
        tbFilter.Size = new Size(100, 20);
        Controls.Add(tbFilter);
    }
}

In this example, I have added a TextBox control to the form and positioned it at the top-left corner of the form.

Step 3: Add a TextChanged Event for the TextBox Control

Next, you need to add a TextChanged event for the TextBox control. This event will be called each time the text in the TextBox changes. In the event handler, you need to filter the ListView using the Contains function.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        // Create columns for the ListView
        listView1.Columns.Add("Name", 100);
        listView1.Columns.Add("Age", 50);
        listView1.Columns.Add("Country", 100);

        // Add items to the ListView
        listView1.Items.Add(new ListViewItem(new string[] { "John", "25", "USA" }));
        listView1.Items.Add(new ListViewItem(new string[] { "Sarah", "30", "Canada" }));
        listView1.Items.Add(new ListViewItem(new string[] { "David", "21", "Australia" }));
        listView1.Items.Add(new ListViewItem(new string[] { "Sophie", "27", "France" }));
        listView1.Items.Add(new ListViewItem(new string[] { "Lisa", "22", "UK" }));

        // Create a TextBox control
        TextBox tbFilter = new TextBox();
        tbFilter.Location = new Point(10, 10);
        tbFilter.Size = new Size(100, 20);
        tbFilter.TextChanged += TbFilter_TextChanged;
        Controls.Add(tbFilter);
    }

    private void TbFilter_TextChanged(object sender, EventArgs e)
    {
        string filter = ((TextBox)sender).Text.ToLower();
        foreach (ListViewItem item in listView1.Items)
        {
            if (!item.Text.ToLower().Contains(filter))
            {
                item.Remove();
            }
        }
    }
}

In this example, I have added a TextChanged event for the TextBox control and implemented the event handler. In the event handler, I first convert the filter text to lowercase to make the comparison case-insensitive. Then, I loop through all the items in the ListView and remove the ones that do not contain the filter text.

Conclusion

In summary, filtering a ListView using a 'Contains' function in C# is a useful technique to help users locate specific items in a ListView control. I hope this code example has been useful for you, and you can use this technique in your Windows Forms applications.