简体   繁体   English

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

[英]Remove Listbox Items With Checkboxes

I am aware of the question asked by user ElPeta. 知道用户ElPeta问的问题。 However the selected answer to his question does not fix my issue. 但是,对他的问题的选定答案不能解决我的问题。

In my program I have a list box, whenever a check box is clicked and its checked property set to true, the list box is populated with text, however when the check box is unchecked, I want to remove the text associated with the check box from the list box. 在我的程序中,我有一个列表框,每当单击一个复选框并将其checked属性设置为true时,该列表框就会填充文本,但是当未选中该复选框时,我想删除与该复选框关联的文本从列表框中。

Example Before and After: 之前和之后的示例:

之前 后

My Code: 我的代码:

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

    }
}

The ListBox.ObjectCollection returned from ListBox.Items has methods Remove(obj) and RemoveAt(index) . ListBox.ObjectCollection返回的ListBox.Items具有方法Remove(obj)RemoveAt(index) You can loop it backwards and use RemoveAt : 您可以向后循环并使用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);
        }
    }
}

I have also used String.IndexOf instead of Contains to support case-insensitivity. 我还使用String.IndexOf而不是Contains来支持不区分大小写。

I don't know your specific error, but I imagine you might have an issue since you're using the item count of the listbox, and removing the items. 我不知道您的特定错误,但我想您可能会遇到问题,因为您正在使用列表框的项目计数并删除项目。 When the item gets deleted the item count is changed. 删除项目后,项目计数将更改。 I believe there's a method for list box such as listbox.BeginUpdate() you can call before making changes to the items within the listbox and listbox.Update() after you're finished. 我相信有一种列表框的方法,例如listbox.BeginUpdate(),您可以在完成后对listbox和listbox.Update()中的项目进行更改之前调用它。 Let me know if I'm in the right area with your problem 让我知道我是否在您遇到的问题中

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM