簡體   English   中英

刪除帶有復選框的列表框項目

[英]Remove Listbox Items With Checkboxes

知道用戶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);
                }
            }
        }

    }
}

ListBox.ObjectCollection返回的ListBox.Items具有方法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來支持不區分大小寫。

我不知道您的特定錯誤,但我想您可能會遇到問題,因為您正在使用列表框的項目計數並刪除項目。 刪除項目后,項目計數將更改。 我相信有一種列表框的方法,例如listbox.BeginUpdate(),您可以在完成后對listbox和listbox.Update()中的項目進行更改之前調用它。 讓我知道我是否在您遇到的問題中

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM