TechyMedia

A .NET Technology blog | .NET,C# Progamming,C# tutorials,WPF WCF|Software Engineer

NAVIGATION - SEARCH

Create User control for C# winforms applications

User controls are similar to any other class in .NET. But the difference is that user controls are always derived from the UserControl class in System.Windows.Forms namespace.User controls can be created directly inside your project or for re-usability and better maintainability it is suggested to create user controls as separate dll,Windows Forms Control Library.

User controls are normally a combination of  more than one control in a single logical unit for achieving some specific functionality and to improve the reusabilty.User controls are similar to any other class in .NET. But the difference is that user controls are always derived from the UserControl class in System.Windows.Forms namespace. User controls are segregated in to partial classes for seperating the logic from designer.User controls can be created directly inside your project or for reusability and better maintainability it is suggested to create user controls as seperate dll,Windows Forms Control Library.

The various steps of creating a C# windows Forms user control is detailing below.

1. Open the Visual Studio and start a new project.Select windows Control Library from Visual studio templates for Windows   applications.

2.  Name the project as you desired(Here I named as UserControlLibrary) and click OK.

3. Usercontrol1 will be created automatically.

4. Change the name to your desired name (Here I named LoginControl). Remember to rename it at all places properly.

5. Create your user control User Interface.In this tutorial I am just writing a small login control as below.Name the 2 textboxes as txtUserId and txtPassword and button as btnLogin

6. Write the user control code logic.An example given below (Yes,it is a stupid logic just to explain only)

I have given 2 properites to the user control,UserId and Password which user can set to some values through the Property Window once after drag & drop the Usercontrol  in the form.In real scenarios this properties shall be set by values retrieved from database or any other data store.

using System;
using System.Windows.Forms;is 

namespace UserControlLibrary
{
    public partial class LoginControl : UserControl
    {
        public LoginControl()
        {
            InitializeComponent();
        }

       private string userid;
        public string UserId
        {
            get
            {
                return userid;
            }
            set
            {
                userid = value;
            }
        }
 
        private string password;
        public string Password
        {
            get
            {
                return password;
            }
            set
            {
                password = value;
            }
        }

During Login button click user credentials are compared with the user entered login credentials in the textbox controls txtUserId and txtPassword. Hard-coded values are assigned to Properties UserID and Password during design time.If both matches user will be allowed to login. Use your authentication mechanism(database driven or anything) instead of the sample here.Idea of this article is only to explain how to create UserControl with some properties and consume it.

 private void btnLogin_Click(object sender, EventArgs e)
        {
            if (UserId == txtUserId.Text && Password == txtPassword.Text) 
            {
                MessageBox.Show("Login Successful");
            }
            else
                MessageBox.Show("Login Failed.Invalid Credentials");
        }
  } 
}

 7. Build the user control.Once built,the user control will be visible in the toolbox at top as below.This user control is ready for use now.

Test the user control

1. Create New Windows Forms Project,I named the project as TestUserControl

2. Drag & drop the User control to the form.

3.Select the Usercontrol and go to Properties window.Set UserID as "Techymedia" and Password to "Rajeev"(hardcoded to make the logic simple)

4. Set the TestUserControl project as startup project.

5. Run the application.You can see the following result as per the code logic.If enetered User ID as "TechyMedia" and Password as "Rajeev"  the login successful message will be shown and invalid login in other cases.

Most of the time you need to debug the usercontrol at design time.To understand debugging user control at design time read the article

Design time debugging of User controls

 

Comments (2) -

hi How r you???

Reply

you also have to add user control in the Toolbox(Visual Studio) , to do so you would have to add "choose items" from toolbox and browse the file where you created usercontrol1 ( UserControl1 file>> bin>> debug>> select .dll file to add ), now your control would be added in your toolbox and just drag and drop your control to your Form.designer.

Regards:
Ali Zia

Reply

Pingbacks and trackbacks (1)+

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading