Add/Remove User Control for C#

Posted by Mike Gold Articles | Windows Forms C# January 18, 2000
A nice feature in the Visual.NET environment is the ability to create User Controls.
Reader Level:
Download Files:

A nice feature in the Visual.NET environment is the ability to create User Controls.  Below is the files for the Visual.NET Add/Remove project:

The AddRemoveControl.cs is the User Control code for the Add Remove control and the Form1.cs code shows you how to implement this control in a form.

To create a user control is fairly easy. In the Project menu, you choose User Control, and it will add a custom control to your existing project as a separate class. This example is a user control that has two list boxes that can transfer strings back and forth between them. In essence, you are 'adding'  and 'removing' strings from one list into another.

The Source List (Inside Barn) is initially populated through the user controls populate method ( In AddRemoveControl.cs ):

public void PopulateSource(string[] list)
SourceListbox.Items.All = list;

The rest of the functionality of the Add/Remove Control is initiated by the Add, Remove buttons. If the Add button is pressed, items selected in the source list box are moved to the destination list box. The code for this is illustrated below:

protected void AddButton_Click (object sender, System.EventArgs e)
// copy selected strings in the source list to the destination list
for (int i = 0; i < SourceListbox.SelectedItems.Count; i++)
// remove selected strings from the source list
for(int j = SourceListbox.SelectedIndices.Count - 1; j >= 0; j --)

Also noteworthy in the User Control is the use of the ArrayList type as a property for extracting data from the lists in the control:

private ArrayList m_DestList = new ArrayList();
public ArrayList DestinationList
for (int i = 0; i < DestinationListbox.Items.Count; i++)
return m_DestList;

The ArrayList is a C# dynamic collection that works much the same as the old MFC CObArray collections, but is a bit easier to use.

In the property shown above, the array list is cleared and repopulated from the destination list box. We can see the use of this property for retrieving data from our control in the form method below. This method is called in the form, when the result button is pushed.

private void DrawResult()
// get a reference to the graphics object in the form
Graphics g = this.CreateGraphics();
// get the data from the user controls destination list box
ArrayList myList = this.addRemoveUserControl1.DestinationList;
// create font, pens, and brushes for displaying strings from user control
Font myFont = new Font("Arial", 8);
Pen myPen =
new Pen(this.BackColor);
SolidBrush myBrush =
new SolidBrush(Color.Blue);
SolidBrush backBrush =
new SolidBrush(this.BackColor);
// erase old result
Rectangle rect = new Rectangle(ClientRectangle.Left, ClientRectangle.Bottom - 21,ClientRectangle.Right, ClientRectangle.Bottom);
g.DrawRectangle (myPen, rect);
g.FillRectangle(backBrush, rect);
int sumLength = 0;
// display strings from the user control at the bottom of the form
for (int i = 0; i < myList.Count ; i++)
g.DrawString(myList[i].ToString(), myFont, myBrush, sumLength*myFont.Height,
this.ClientRectangle.Bottom - 20);
sumLength += myList[i].ToString().Length;
// free resources

Although not done in this project, the Add/Remove control could be made more useful by adding persistence to the add/remove functionality such as writing the source and destination list to a text file and reading it back it when the program is restarted. Also it would be nice if the list box and buttons resized as the control was sized. Perhaps we'll tackle this in a future article.

Employers - Post Free Jobs