User Control in C#

Here I have to show the complete demo of How to create user control in C# window application. I have to show complete description of user control and basics of user control. After learning this document you have to be able to make user control according to own requirement. I have to cover below listed topic:

  1. Why we make User control?
  2. User control complete demo.
  3. Advantage of using User control.
  4. How to use user control?

1. Why we make User control

This Provide additional re-use flexibility with large scale web project. It’s also help to find bug and resolve bug in short time. If you want some changes in your code then you have to write code at one place (user control) that effect in every web form or every Form of window application. Using this technique you can save your extra effort and also save your time.

2. User control Complete Demo


Here I have to show the Demo make user control in window application C#.

Go to=>VS2010=>File=>Project



Go to=>Widows=>Windows Form Controls library

Write your library name and click ok.



After that I have to write code in this library.

Here I have to Show demo: My requirement is I want combo box binded with Some State Name. and because I have to use this combobox in multiple form of window application. So I have to create this user control.

1 . My design Part:Here I have drag and drop the Combobox from toolbox.



Code of:.cs file

  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. using System.Data;  
  10. namespace mycontrolLibrary  
  11. {  
  12.     public partial class MyCombobox : UserControl  
  13.     {  
  14.         #region Public Member  
  15.         /// <summary>  
  16.         /// Here i have create two property for geting  value of selected item of comboBox  
  17.         /// and geting text of selected item of comboBox.  
  18.         /// cmbState is a name of combobox.  
  19.         /// its neccesary property its used when you use this control  
  20.         /// in your form.  
  21.         /// </summary>  
  22.         public string SlectedText  
  23.         {  
  24.             get { return cmbState.Text; }  
  25.         }  
  26.         public string Selectedvalue  
  27.         {  
  28.             get { return cmbState.SelectedValue.ToString(); }  
  29.         }  
  30.         /// <summary>  
  31.         /// Here i have to declare event.Like Normal combobox selected indexChanged event.  
  32.         /// you can use this event whenever you have to require some action/operation on selected index  
  33.         /// changed of combobox.you have to fired this event and perform some action  
  34.   
  35.         /// </summary>  
  36.         public event EventHandler SelectedIdexChanged;  
  37.         #endregion  
  38.  
  39.         #region Constructor  
  40.         /// <summary>  
  41.         /// MyCombobox is a constructor.  
  42.         /// Inside constructor fire the load event of this usercontrol.  
  43.         /// </summary>  
  44.         public MyCombobox()  
  45.         {  
  46.             InitializeComponent();  
  47.             this.Load += new EventHandler(MyCombobox_Load);  
  48.             this.cmbState.SelectedIndexChanged += new EventHandler(cmbState_SelectedIndexChanged);  
  49.         }  
  50.         /// <summary>  
  51.         /// Combobox Selected indexChanged event of Combobox  
  52.         /// </summary>  
  53.         /// <param name="sender"></param>  
  54.         /// <param name="e"></param>  
  55.         void cmbState_SelectedIndexChanged(object sender, EventArgs e)  
  56.         {  
  57.             if (SelectedIdexChanged != null)  
  58.                 SelectedIdexChanged(sender, e);  
  59.         }  
  60.         #endregion  
  61.         #region User Control Event  
  62.         void MyCombobox_Load(object sender, EventArgs e)  
  63.         {  
  64.             BindComboBox();  
  65.         }  
  66.         #endregion  
  67.         #region Binding Method  
  68.         /// <summary>  
  69.         /// This method bind combobox with specified data.  
  70.         /// </summary>  
  71.         private void BindComboBox()  
  72.         {  
  73.             DataTable dtState = new System.Data.DataTable();  
  74.             dtState.Columns.Add("txtPart");  
  75.             dtState.Columns.Add("valuePart");  
  76.             dtState.Rows.Add("Delhi""1");//Here Delhi is txtPart and 1 is valuepart  
  77.             dtState.Rows.Add("Bihar""2");  
  78.             dtState.Rows.Add("Punjab""3");  
  79.             dtState.Rows.Add("UP""4");  
  80.             cmbState.DataSource = dtState;  
  81.             cmbState.DisplayMember = "txtPart";  
  82.             cmbState.ValueMember = "ValuePart";  
  83.         }  
  84.         #endregion  
  85.     }  
  86. }   
3. Advantage of using User control
  • Code re-usability.
  • Time saving.
  • Less effort.
  • Easy to find Bug and fix it.
  • Save memory also.

4. How to use User Control

Go to=>Tool Box =>right click=>Select Add tab option



After selecting add tab option write your tab name: my tab name is tets



Then Go To=>tets(Your tab) and =>Right click=>select=>Choose item



Click=>Browse button



Add your control library dll=>Select dll file and click on open button.



Here you can see your user control library has been added=>after that click ok.



After that you can see your control inside tets tab:



After adding control in tool box you can drag and drop control in your form where you want to use this control.

Its my user control you can make own user control according to own requirement.



If you want to get the custom control selected value and selected text write this code.

Here I have only show how you use property defined in custom user control.


  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.   
  10. namespace testapp  
  11. {  
  12.     public partial class Form1 : Form  
  13.     {  
  14.         public Form1()  
  15.         {  
  16.             InitializeComponent();  
  17.             myCombobox.SelectedIdexChanged += new EventHandler(myCombobox_SelectedIdexChanged);  
  18.         }  
  19.   
  20.         void myCombobox_SelectedIdexChanged(object sender, EventArgs e)  
  21.         {  
  22.             MessageBox.Show(myCombobox.SlectedText);  
  23.         }  
  24.   
  25.         private void btnDropdownSelectedvalue_Click(object sender, EventArgs e)  
  26.         {  
  27.             MessageBox.Show(myCombobox.Selectedvalue.ToString());  
  28.         }  
  29.   
  30.         private void btnDropDownSelectedText_Click(object sender, EventArgs e)  
  31.         {  
  32.             MessageBox.Show(myCombobox.SlectedText);  
  33.         }   
  34.     }  
  35. } 
Happy coding.