Posted: January 4th, 2010 By: Michael Martin
In this post I will show you how to loop through two related lists of items (the parent and the child) and filter items out of the parent list that exist in the child list using C#. This technique is helpful when a user needs to select from a discrete list of items into another list that cannot contain duplicates.
Download the Visual Studio Project Files Here
Please note: It's in 7zip Format (Free Zip Program).
Here's a view of the code used in the video above:
Form1.cs
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 NestedLoopsItemFiltering
{
public partial class Form1 : Form
{
private ObjectCollection _collection;
public Form1()
{
InitializeComponent();
_collection = new ObjectCollection(comboBox1.Items.Count);
for (int i = 0; i < comboBox1.Items.Count; i++)
{
_collection[i] = comboBox1.Items[i];
}
}
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem != null)
{
listBox1.Items.Add(comboBox1.SelectedItem);
FilterItems();
comboBox1.SelectedIndex = -1;
comboBox1.Text = String.Empty;
}
}
private void button2_Click(object sender, EventArgs e)
{
if (listBox1.SelectedItem != null)
{
listBox1.Items.Remove(listBox1.SelectedItem);
FilterItems();
comboBox1.Focus();
}
}
private void FilterItems()
{
ComboBox.ObjectCollection parent = comboBox1.Items;
ListBox.ObjectCollection child = listBox1.Items;
parent.Clear();
for (int i = 0; i < _collection.Count; i++)
{
parent.Add(_collection[i]);
}
for (int i = 0; i < child.Count; i++)
{
for (int j = 0; j < parent.Count; j++)
{
if (child[i].ToString() == parent[j].ToString())
{
parent.RemoveAt(j);
j = parent.Count;
}
}
}
}
}
}
ObjectCollection.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace NestedLoopsItemFiltering
{
class ObjectCollection : IList
{
private object[] _collection;
public ObjectCollection(int capacity)
{
_collection = new object[capacity];
}
public int Add(object value)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(object value)
{
throw new NotImplementedException();
}
public int IndexOf(object value)
{
throw new NotImplementedException();
}
public void Insert(int index, object value)
{
throw new NotImplementedException();
}
public bool IsFixedSize
{
get { throw new NotImplementedException(); }
}
public bool IsReadOnly
{
get { throw new NotImplementedException(); }
}
public void Remove(object value)
{
throw new NotImplementedException();
}
public void RemoveAt(int index)
{
throw new NotImplementedException();
}
public object this[int index]
{
get
{
return this._collection[index];
}
set
{
this._collection[index] = value;
}
}
public void CopyTo(Array array, int index)
{
throw new NotImplementedException();
}
public int Count
{
get { return this._collection.Count(); }
}
public bool IsSynchronized
{
get { throw new NotImplementedException(); }
}
public object SyncRoot
{
get { throw new NotImplementedException(); }
}
public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
}
}
Enjoy this article? Please comment below and subscribe to the RSS feed











