OrdListBox is a class derived from
was written by me when I was working on a project where I had this requirement
of having ordered items in a list box. The issue was that there might be
multiple copies of the same item in the list box and I had no way to
differentiate among these duplicate items. The duplicate items would obviously
be textual abstractions of distinct underlying objects. MFC had
which allowed us to associate a 32 bit value with
an item in the list box. I assumed rather enthusiastically that there would be
something analogous to that in the .NET
was with an intense annoyance that I realized that I couldn't find anything to
do the job. Of course for all I know there might be some hidden feature
somewhere which I had overlooked and missed. But anyway I had to write my own
class to achieve what I wanted to. I was a little stuck when Shog9 brilliantly
helped me out with a beautiful but simple suggestion that I totally missed out
The class basically allows you to associate strings with each list box item.
Now when you add an item to the string using the new method added to the derived
class, this order string will be used to determine the position where this new
item would be inserted at. The order string can be either numeric or
non-numeric. If the order string is numeric then 6 comes before 12, but if the
order string is non-numeric you must watch out, because 6 is now greater than 12
on account of it's position in the character set.
OrdListBox listBox1 = new OrdListBox(true);
Initializes a new instance of the
public OrdListBox( bool bNum );
bNum - Set this to
true to make
the ordering numeric and to
false to make the ordering
This will add a new ordered item to the ordered list box, and it will
insert the new item at the correct position within the list box, that will
maintain the order of the items in the list box, after the new item has been
added to it.
public void AddOrderedItem(string ItemText, string ItemOrder);
ItemText - This is the text of the item to be added to the
ordered list box.
ItemOrder - This is the order string that will specify the
order value of the item to be added.
This method will return the order value of an item in a list box given the
index of the item within the list box.
public string GetItemOrder(int index)
index - This is the index of the required item within the
The method will return a string that represents the order value of the
specified list box item.
public class OrdListBox : System.Windows.Forms.ListBox
public OrdListBox(bool bNum)
bNumeric = bNum;
Sorted = false;
public void AddOrderedItem(string ItemText, string ItemOrder)
ItemData data = new ItemData(ItemText,ItemOrder);
int c = Items.Count;
for (int i = 0; i < Items.Count; i++)
if( c == Items.Count)
public string GetItemOrder(int index)
if(index < 0 || index >= Items.Count)
private bool bNumeric = false;
public ItemData(string str1, string str2)
private string m_ItemText;
private string m_ItemOrder;
public override string ToString()
public bool IsLess(ItemData d, bool bNum)
int i1 = Convert.ToInt32(m_ItemOrder);
int i2 = Convert.ToInt32(d.m_ItemOrder);
return ( i1 < i2 );
return ( m_ItemOrder.CompareTo(d.m_ItemOrder) < 0 );
public string ItemOrder
Nish Nishant is a Principal Software Architect based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.
Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.
: If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish