Creating user control

  Creating and using Windows Forms Control Library in C#.Net 

To create User Control in C# select “Windows Forms Control Library” icon from New Project Dialog Box.

Add some controls and add codes for the controls in User Control.

Example

Creating and using Windows Forms Control Library in C#.Net

 

Code
private void btnColour_Click(object sender, EventArgs e)
        {
//this will change the color of form on which this control is placed.
           this.ParentForm.BackColor= Color.Red;
        }

Now once the control is created we need to built it in order to use in other projects.

Using User Control in project

To use user control in our project we need to add that control in project. To add right click on Toolbox and select “Choose Items”

Creating and using Windows Forms Control Library in C#.Net

A dialog box “Choose Toolbox Items” will appear. In that dialog box click browse button and select user control created earlier from bin\debug folder of user control project, then press Ok.

Creating and using Windows Forms Control Library in C#.Net

User control will be added to the project toolbox.

Creating and using Windows Forms Control Library in C#.Net

Now we can use the user control. 

Example

Creating and using Windows Forms Control Library in C#.Net 

 Screen shot after Execution

Creating and using Windows Forms Control Library in C#.Net

Color of the form changes on button click without doing any coding in the current form. On button click code of user control is executed.


  Modified On Jun-23-2018 07:13:57 AM

Leave Comment