Make Custom Control or Component

This article explains how to make your own custom control in C#. Here in this example I will make a Login Control. To do that open Visual Studio then select "File" -> "New" -> "Project..." then select Windows Forms Control Library.



Image 1.

Now I design my control with User Id and Password Label and Text Box.



Image 2.

The following is my login control code:

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Drawing;  
  5. using System.Data;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9.   
  10. namespace MakeOwnCustomControl  
  11. {  
  12.     public partial class UserControl1 : UserControl  
  13.     {  
  14.         public delegate void loginOper();  
  15.         public event loginOper checkValue;  
  16.   
  17.         public string userID  
  18.         {  
  19.             set  
  20.             {  
  21.                 txtUserId.Text = value;  
  22.             }  
  23.             get  
  24.             {  
  25.                 return txtUserId.Text;  
  26.             }  
  27.         }  
  28.         public string password  
  29.         {  
  30.             set  
  31.             {  
  32.                 txtPassword.Text = value;  
  33.             }  
  34.             get  
  35.             {  
  36.                 return txtPassword.Text;  
  37.             }  
  38.         }  
  39.   
  40.         public UserControl1()  
  41.         {  
  42.             InitializeComponent();  
  43.         }  
  44.   
  45.         private void btnLogin_Click(object sender, EventArgs e)  
  46.         {  
  47.             if (txtUserId.Text == "" && txtPassword.Text == "")  
  48.             {  
  49.                 MessageBox.Show("User ID & Password can not be null.");  
  50.             }  
  51.             else if (txtUserId.Text == "")  
  52.             {  
  53.                 MessageBox.Show("Please Enter User ID.");  
  54.             }  
  55.             else if (txtPassword.Text == "")  
  56.             {  
  57.                 MessageBox.Show("Please Enter Password.");  
  58.             }  
  59.             else  
  60.             {  
  61.                 if (checkValue != null)  
  62.                 {  
  63.                     checkValue();  
  64.                 }  
  65.                 else  
  66.                 {  
  67.                     MessageBox.Show("Please Enter correct UserId/Password");  
  68.                 }  
  69.             }  
  70.         }  
  71.     }  

Now compile and run this app.



Image 3

Here click on Load and copy the DLL path.



Image 4.

Now create a new Windows application.

Open Visual Studio then select "File" -> "New" -> "Project..." then select Windows Application.



Image 5.

Here select View-> Toolbox then right-click anywhere and select "Choose Items...".



Image 6.

A popup window will open. Click on Browse.



Image 7.

Go to the User Control DLL location (we copied the DLL path earlier). Select the DLL then click Open.



Image 8.

Now you can see your control then click OK.



Image 9.

Now go to your form. See that the control is displaying in the tool box. Drag and drop this control on the form.



Image 10.

Now for the code part of this form.

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Windows.Forms;  
  9. using MakeOwnCustomControl;  
  10. using System.Data.SqlClient;  
  11.   
  12. namespace MyTestApplication  
  13. {  
  14.     public partial class Form1 : Form  
  15.     {  
  16.         SqlDataAdapter da;  
  17.         DataSet ds;  
  18.         SqlConnection con;  
  19.   
  20.         UserControl1 myCustomControl;  
  21.   
  22.         public Form1()  
  23.         {  
  24.             myCustomControl = new UserControl1();  
  25.             InitializeComponent();  
  26.         }  
  27.   
  28.         private void Form1_Load(object sender, EventArgs e)  
  29.         {  
  30.             myCustomControl.checkValue += new MakeOwnCustomControl.UserControl1.loginOper(UserControl1_checkValue);  
  31.         }  
  32.   
  33.         void UserControl1_checkValue()  
  34.         {  
  35.             con = new SqlConnection(@"DATA SOURCE=MyPC\SqlServer2k8;INTEGRATED SECURITY=TRUE;DATABASE=TEST");  
  36.             da = new SqlDataAdapter("Select * FROM tbl_User Where UserId='" + myCustomControl.userID + "' AND Password='" + myCustomControl.password + "'", con);  
  37.             ds = new DataSet();  
  38.             da.Fill(ds);  
  39.   
  40.   
  41.             if (ds.Tables[0].Rows.Count > 0)  
  42.             {  
  43.                 MessageBox.Show("Welcome !" + ds.Tables[0].Rows[0]["Name"].ToString());  
  44.             }  
  45.             else  
  46.             {  
  47.                 MessageBox.Show("Please enter correct UserId/Password");  
  48.             }  
  49.         }  
  50.     }  

Here I am authenticating User ID and Password from my SQL Table. Now run the application.

If you don't enter an id and password and click on the Log In button then the following error message will appear.



Image 11.



Image 12.



Image 13.

The following is my data table design from which I am authenticating the user.



Image 14.

Up Next
    Ebook Download
    View all
    css
    Read by 0 people
    Download Now!
    Learn
    View all