Handy List Selection Control that We Use Every Day


I still remember the difficulty that I faced the first time I tried to design something like this in VC++ and ASP.NET. It was annoying and I always wondered why there is no control like this available in the world when it is used widely in almost all the applications that we develop. Later, with the power and simplicity of C#, I thought, I can write and help others to do their job more easily and effectively.

For simplicity, I call the left side List box as the source and the right side as the destination. You need to supply the input to fill the source List box. It takes a collection (ArrayList) as an argument. Your interest in this control is the items that are selected and also returned as an ArrayList. Because it is a collection that deals with objects, you can fill any data you want in the list box.

The array list is added directly to the List box instead of adding each item one by one.

private void AddElements(System.Windows.Forms.ListBox tempLst,
ArrayList tempArray)
int intMaxElements = tempArray.Count;
object[] bunchOfStuff = new object[intMaxElements];
bunchOfStuff = tempArray.ToArray();


If your application deals with Arrays, it is quite easy to convert from Array to ArrayList using C#’s built-in functions.

The control exposes the following Methods:

  1. //public void SetSourceData(ArrayList sourceArray)

  2. GetDestData     //public ArrayList GetDestData()

  3. //public void SetDestData(ArrayList DestArray)

  4. GetSourceData   //public ArrayList GetSourceData()

The first two methods should suffice for your general use. But later, I thought there may be cases where the destination data will be filled and later it must be modified (this generally happens when you are editing your saved data). So, the latter two methods will be useful in that case.

The Control Properties:

  1. SourceLabel    //to set/get the Source List box Label

  2. //to set/get the Destination List box Label

  3. ItemText       //to set/get the Group box label

I set these properties using the Properties Window, but if you like you can set them dynamically at runtime.

I implemented the resize event for the control so that the control will fit for your required size. I set a minimum size for the control so that it will not be set to too small.

Note: I was doing some simple arithmetic in the resize function, but this may not be required for you to understand.

A double-click event is implemented on both the list boxes so that you don’t need to select the item and then click the buttons. When selecting and double-clicking any Item in the List box, it will be added automatically to the other List box.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read