How to Create USER CONTROL in C# Windows Application


In my Previous Article's :

  1. Datagridview Export to Excel in C#.NET
  2. Speech Translator in C#.NET
  3. Generate Barcode in C#.NET
  1. A user control is nothing but, user defined control.
  2. That means you can create our own control, and you can use it in any form, wherever required.
  3. The user control may contain some user interface with controls like buttons, textboxes etc.
  4. So finally, you need to design the user interface only once in the "User Control", and you can use it any no. of times, in any form.
  5. Advantage : Avoids repetition of design and code.
  6. Generally, you can use it for designing the common header for all the forms in the project (as given in the below example).

Difference Between User Controls & Form

Sl.No User Control Form
1 It‘s a container for other controls. It‘s also a container for other controls.
2 It can‘t run individually. It can run individually.
3 It inherits a pre-defined class called "System.Windows.Forms.UserControl" It inherits a pre-defined class called "System.Windows.Forms.Form"
4 It is meant for re-usability. It is meant for direct execution.

Implementation of User Controls

  • Create User Control

    1. Click on "Project" menu "Add User Control".
    2. Enter the name of the new user control.
    3. Click on "Add".
  • Design and Develop the User Control

    1. Design the UI in the user control, by dragging the controls from the toolbox.
    2. Write the code in "Code window". (User control also supports event handlers similar to forms).
  • Invoke the User Control

    1. Open the required form and build the project.
    2. Then the user control name will be displayed in the toolbox automatically.
    3. To invoke the user control on a form, just drag it from the toolbox into the form designer.
    4. Then the control object will be created in the form.

User Controls Demo Project

  • Create the Windows Application Project.
  • Click "Project" menu – "Add User Control".
  • Enter the name as "Title".
  • Click on "Add"
  • Then design the user control as follows :
create user controls application in c#
  1. Set the properties of "Timer1"
    • Enabled: True
    • Interval: 1000
  2. Double click on the user control and write the code.
  3.      private void Title_Load(object sender, EventArgs e)
           {
            lbldate.Text = DateTime.Now.ToLongDateString();
            lbltime.Text = DateTime.Now.ToLongTimeString();
           }
       
  4. Double click on the user control and write the code.
  5.      private void timer1_Tick(object sender, EventArgs e)
          {
            lbldate.Text = DateTime.Now.ToLongDateString();
            lbltime.Text = DateTime.Now.ToLongTimeString();
          }
       
  6. Come back to the "Form1" and "Build" the project.
  7. Then the user control name "Title" will be added to the toolbox, at the top.
  8. Then drag and drop it from the toolbox into the form. Then it‘s ready.
  9. In the similar way, you can drag and drop the controls into any no. of forms, within the same project.
  10. Note : The current user control is called as "Local User Control". So that it can be used within the same project only. You can‘t use it in other windows application projects. But if you want to utilize the same user control in other windows application projects also, then you have to create this user control in "Windows Forms Control Library".
Tag : C#, C# Tutorials
0 Komentar untuk "How to Create USER CONTROL in C# Windows Application"

Back To Top