Handy List Selection Control that We Use Every Day

by Nagavardhan Ponnada

A custom control that is useful for selecting/ deselecting a sub set of list items. Also learn how simple it is to create custom controls using C#.


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.

About the Demo Project

I am a big fan of cricket and I chose to select my own "fantasy" team of 11 from the available players to show the demo.


  1. Download the source code and unzip it.
  2. Open VS.NET's Tool menu and select "Add/Remove Toolbox Items".
  3. Click "Browse" and navigate to the SelectListCtrl.dlll in the bin\Release directory of the source code.
  4. Click Open and then OK; the SelectListCtrl.dll control will now be in your Toolbox. The selectListCtrl will appear in General section of the tool box. Just drag the control onto your form to use it.
  5. [ToolBox.JPG]

  6. You can resize the control to the size that fits your requirement and space on your application's User Interface.
  7. Set the control's following properties, using the Properties Window of the control.
    • Source Label (This will set the label for the first List box).
    • DestinationLabel(This will set the label for the second List box)
    • ItemText (This will set the label for the Group Box that is containing the List box).


Filling Data to the Control

You can fill your data in an arraylist. Just call the control's SetSourceData method by passing the ArrayList.

ArrayList Players = new ArrayList();
   Players.Add("Sachin Tendulkar");
   Players.Add("Virender Sehwag");

   Players.Add("Rohith Sharma");
   Players.Add("Mohammad Kaif");


Reading the Selected Data from the Control

I implemented a Click event for the OK button so that you will know how to read the selected data. I am just displaying a message box where all the selected players' names are displayed.

ArrayList SelectedPlayers = new ArrayList();
   SelectedPlayers = this.userControl11.GetDestData();
   string result = "" ;
   foreach (string i in SelectedPlayers)
      result += i + "::::";

I hope you will enjoy using this control.

This article was originally published on Monday Apr 21st 2008
Mobile Site | Full Site