C# 删除带有复选框的列表框项目

C# 删除带有复选框的列表框项目,c#,linq,checkbox,listbox,C#,Linq,Checkbox,Listbox,我知道用户ElPeta提出的问题。但是,对他的问题选择的答案并不能解决我的问题 在我的程序中,我有一个列表框,每当单击一个复选框并将其checked属性设置为true时,列表框都会填充文本,但是当该复选框未选中时,我希望从列表框中删除与该复选框关联的文本 前后示例: 我的代码: using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.

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

在我的程序中,我有一个列表框,每当单击一个复选框并将其checked属性设置为true时,列表框都会填充文本,但是当该复选框未选中时,我希望从列表框中删除与该复选框关联的文本

前后示例:

我的代码:

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

    }
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用系统数据;
使用系统图;
使用System.Linq;
使用系统文本;
使用System.Windows.Forms;
名称空间CarFinderByMake
{
公共部分类frmCFBM:表单
{
公共frmCFBM()
{
初始化组件();
}
私有void Form1\u加载(对象发送方、事件参数e)
{
//TODO:这行代码将数据加载到“honestralphusedcarsdataset.tblCars”表中。您可以根据需要移动或删除它。
this.tblCarsTableAdapter.Fill(this.honestralphusedcarsdataset.tblCars);
}
私有void chkFord_CheckedChanged(对象发送方,事件参数e)
{
如果(chkFord.Checked)
{
//lstCarMakes.Items.Clear();
//填充列表框
var fordCars=从HoneStralphUsedCarsDataSet.tblCars中的x开始,其中x.Make.StartsWith(“Ford”)选择x;
foreach(fordCars中的var x)
{
lstCarMakes.Items.Add(x.ModelYear+“”+x.Make+“-”+x.Color);
}
}
其他的
{

对于(int x=0;x我不知道您的具体错误,但我想您可能会有问题,因为您正在使用列表框的项目计数并删除项目。当项目被删除时,项目计数会更改。我相信列表框有一个方法,例如listbox.BeginUpdate()完成后,您可以在更改listbox和listbox.Update()中的项之前调用。请告诉我您的问题是否在正确的区域中。从
列表框返回的内容。项具有方法
删除(obj)
删除(索引)
。您可以向后循环并使用
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
来支持不区分大小写。

当您尝试删除项目时,什么不起作用?@TimSchmelter我不知道如何获取收集的索引并使用它们来引用我想要删除的行。请替换“Ford”一词使用复选框中的文本或标记值。您正在循环,但未使用循环变量。
lstCarMakes.Items[x]
。另外,向后循环,因为一旦删除一个项目,计数就会减少,索引也会关闭。我没有使用项目计数,但这正是我试图做的。InvariantCultureInogoreCase
有任何先决条件吗?@EvanManning:没有。你也可以使用
StringComparison.CurrentCultureInogoreCase
这里没有太大的区别。这是enum:Than它很奇怪,因为它不允许我使用它,它说
System.StringComparison
没有它的定义。@EvanManning:你真的使用过
InvariantCultureInogoreCase
吗?因为在第一个版本中有一个小的打字错误。你也可以在vis中使用intellisenseual studio将让您提供帮助。@EvanManning:为了澄清,您可以使用
包含
,但它不会找到
ford
,而只找到
ford
。这就是为什么我向您展示了这种方法。更重要的是,如果您希望在循环此集合时通过索引删除项目,则需要反向循环。