Creating a Simple User Control and Adding it to the Toolbox

In this article we will spend some time to learn how to create a simple User Control in C# and add it to the Toolbox.

Creating a New User Control

The following is the procedure for creating a new User Control.

1. Create a new Windows Forms Control Library project from the list of templates available and name it "TimerTickControl".

Window-Form-Application.jpg

2. The following screen will appear for designing our control:

designing Window

3. Add a label control to your form and name it "lblTimerTick" and clear its Text Property.

Label Control

4. Now to add an Icon to the control we will add an image to the project by right-clicking the project then select "Add" -> "Existing Item" then select the image then select "Set it Build Action" -> "Embedded Resource" from the Properties.

5. Rename "UserControl1.cs" to "TimerTick.cs".

6. To add an Icon to the control use the ToolboxBitmap attribute to provide your Control a visual appearance.

namespace TimerTickControl

{

    [ToolboxBitmap(@"D:\CSharp Project Examples\TimerTickControl\TimerTickControl\LiveTimer.bmp")]

    public partial class TimerTick : UserControl

    {

        public TimerTick()

        {

            InitializeComponent();

        }

    }

}

7. Add a Timer Component to your project and set its Enabled Property to "True" and the Interval to "1000".

8. In the Timer_Tick Event add the following code:

private void timer1_Tick(object sender, EventArgs e)
{

    lblTimerTick.Text = DateTime.Now.ToString();

}


The following is the complete code:
 

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Drawing;

using System.Data;

using System.Linq;

using System.Text;

using System.Windows.Forms;

 

namespace TimerTickControl

{

    [ToolboxBitmap(@"D:\CSharp Project Examples\TimerTickControl\TimerTickControl\LiveTimer.bmp")]

    public partial class TimerTick : UserControl

    {

        public TimerTick()

        {

            InitializeComponent();

        }

 

        private void timer1_Tick(object sender, EventArgs e)

        {

            lblTimerTick.Text = DateTime.Now.ToString();

        }

    }

}

9. Build the solution. You can locate the Path at "\bin\debug\".

Build the solution

10. Now let us try to add the control to the project. Create a new Windows Application Project .

new Windows Application

11. Now add a reference ("Project" - > "Add Reference") to the DLL by locating it from the Path where it is. Now the DLL has been added to your project.

Adding DLL

12. Let us add the control to our Toolbox. Add a new tab in control box (here I named it "MyControls").

add the control to our Toolbox

13. Right-Click on the "My Controls" Tab and select "Choose Items". The following dialog appears:

Choose Items in ToolBox

14. Now click "Browse" and select the DLL, the control with the icon is displayed as shown here:

Browse and select the DLL

15. Click "OK" to add the control to the Toolbox. That is, our control is ready to be added to the project.

16. Now try adding the control to the project by double-clicking on it.

adding control to the project

17. Once the control has been added, it will show a running timer text in the control.

Up Next
    Ebook Download
    View all
    Learn
    View all