vendredi 13 novembre 2015

Remove Listbox Items With Checkboxes

I am aware of the question asked by user 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 it to remove the text associated with the check box.

Example Before and After:

BeforeAfter

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

    }
}




Aucun commentaire:

Enregistrer un commentaire