使用复选框删除列表框项目

时间:2015-11-13 15:37:20

标签: c# linq checkbox listbox

知道用户ElPeta提出的问题。但是,他的问题的选定答案并不能解决我的问题。

在我的程序中,我有一个列表框,只要单击一个复选框并将其checked属性设置为true,列表框就会填充文本,但是如果取消选中复选框,我想删除与之关联的文本列表框中的复选框。

示例之前和之后:

Before After

我的代码:

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

namespace CarFinderByMake
{
    public partial class frmCFBM : Form
    {
        public frmCFBM()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'honestRalphsUsedCarsDataSet.tblCars' table. You can move, or remove it, as needed.
            this.tblCarsTableAdapter.Fill(this.honestRalphsUsedCarsDataSet.tblCars);

        }

        private void chkFord_CheckedChanged(object sender, EventArgs e)
        {
            if (chkFord.Checked)
            {
                //lstCarMakes.Items.Clear();
                //populating the listbox
                var fordCars = from x in honestRalphsUsedCarsDataSet.tblCars where x.Make.StartsWith("Ford") select x;
                foreach (var x in fordCars)
                {
                    lstCarMakes.Items.Add(x.ModelYear + " " + x.Make + " - " + x.Color);
                }
            }
            else
            {
                for (int x = 0; x <= lstCarMakes.Items.Count; ++x )
                {
                    //here I was attempting to find the index of the items...
                    if (lstCarMakes.Items.Contains("Ford"))
                    {
                        lstCarMakes.Items.IndexOf("Ford");
                        //and after that I was going to remove the items.
                    }
                }
            }
        }

        private void checkBox3_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox3.Checked)
            {
                //lstCarMakes.Items.Clear();
                //populating the list box
                var cadillacCars = from x in honestRalphsUsedCarsDataSet.tblCars where x.Make.StartsWith("Cadillac") select x;
                foreach (var x in cadillacCars)
                {
                    lstCarMakes.Items.Add(x.ModelYear + " " + x.Make + " - " + x.Color);
                }
            }
        }

    }
}

2 个答案:

答案 0 :(得分:1)

ListBox.Items返回的ListBox.ObjectCollection包含方法Remove(obj)RemoveAt(index)。您可以向后循环并使用RemoveAt

private void chkFord_CheckedChanged(object sender, EventArgs e)
{
    if (chkFord.Checked)
    {
        // you have it working ...
    }
    else
    {
        for (int x = lstCarMakes.Items.Count - 1; x >= 0; x-- )
        {
            string car = lstCarMakes.Items[x].ToString();
            if(car.IndexOf("Ford", StringComparison.InvariantCultureIgnoreCase) >= 0)
               lstCarMakes.Items.RemoveAt(x);
        }
    }
}

我还使用String.IndexOf代替Contains来支持不区分大小写。

答案 1 :(得分:0)

我不知道您的具体错误,但我想您可能会遇到问题,因为您正在使用列表框的项目计数,并删除了这些项目。当项目被删除时,项目计数会发生变化。我相信有一个列表框的方法,例如listbox.BeginUpdate(),您可以在更改完列表框和listbox.Update()之后的项目之前调用它们。如果我在您的问题中找到合适的区域,请告诉我