Handy List Selection Control that We Use Every Day | CodeGuru

Handy List Selection Control that We Use Every Day

Introduction 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, […]

Written By
CodeGuru Staff
CodeGuru Staff
Apr 21, 2008
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Introduction

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)
   {
      tempLst.Items.Clear();
      int intMaxElements = tempArray.Count;
      object[] bunchOfStuff = new object[intMaxElements];
      bunchOfStuff = tempArray.ToArray();
      tempLst.Items.AddRange(bunchOfStuff);
   }

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)
    SetSourceData

  2. GetDestData     //public ArrayList GetDestData()

  3. //public void SetDestData(ArrayList DestArray)
    SetDestData

  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
    DestinationLabel

  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.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.