Reputation: 117
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 to remove the text associated with the check box from the list box.
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);
}
}
}
}
}
Upvotes: 1
Views: 734
Reputation: 460058
The ListBox.ObjectCollection
returned from ListBox.Items
has methods Remove(obj)
and RemoveAt(index)
. You can loop it backwards and use 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.
Upvotes: 1
Reputation: 305
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. Let me know if I'm in the right area with your problem
Upvotes: 0