Home > DeveloperSection > Articles > Create simple custom control in C#

Create simple custom control in C#


C# C# 
Ratings:
0 Comment(s)
 4073  View(s)
Rate this:

Create simple custom control in C#

In this article I am trying to explain the concept of custom control with making a small demo.

Steps for creating Custom Control: -

1.       Open Visual Studio 2010.

2.       Create a new project >> select Windows Forms Control Library>> then give appropriate name.

3.       Create a layout of you login page as below image on your custom control.

Create simple custom control in C#

4.  Create two properties for set and get the Form object as System.Windows.Forms.Form type.

   public System.Windows.Forms.Form frmObj { get; set; }

   public System.Windows.Forms.Form thisFrmObj { get; set; }

 

5.  Double click on Submit button to get its click event.

6.  Write the below code in submit button.

a)  private void btnSubmit_Click(object sender, EventArgs e)

b)  {

c)  if (tbUserName.Text.Trim() == "abc" && tbPassword.Text.Trim() == "abc@123")

d)  {

e)  frmObj.Show();

f)  thisFrmObj.WindowState = FormWindowState.Minimized;

g)  thisFrmObj.ShowInTaskbar = false;

h)  tbUserName.Text = tbPassword.Text = "";

i)  }

j)  else

k)  MessageBox.Show("::Invalid User Name and Password::");

l)  }

 

Code Description: -

a)      This is button click event.

c)       Check the user name and password validation.

e)      frmObj.Show() is shows the assigned form object.

f)       This line of code minimized the widowState of thisFrmObj.

g)      This line of code set the false value in thisFrmObj.ShowInTaskbar.

h)      This line of code remove the textbox value.

k)      This is the message box it is shows when enter user name and password is not correct.

7.  Double click on Cancel button to get its click event.

8.  Write the below code in Cancel button.

private void btnCancel_Click(object sender, EventArgs e)

{

thisFrmObj.Close();

}

Now after completing this custom control Goto on solution and Build the solution after building solution this custom control dll file will create on bin folder.

Use the custom control in a demo project:

1.       Open Visual Studio 2010.

2.       Create a new project >> select Windows Forms Application>> then give appropriate name of your demo.

3.       Then Goto on the toolbox Right click on the Tool tab and select Add Tab and give your tab name.

Create simple custom control in C#

 

Create simple custom control in C#

4.       Right click on created new tab and select the choose option for selecting the custom control dll after selecting choose option below image will appear.

Create simple custom control in C#

5.       After appeared above image click on Browser… button to browser your customControl dll file.

6.       After clicking OK button you will see on your custom control is added in the select tab.

7.       Now drag and drop your custom control in the form.

Create simple custom control in C#

8.       Double click on the control and get its load event where we write the code for initialize the form object.

private void customControlLogin1_Load(object sender, EventArgs e)

{

      customControlLogin1.frmObj = new Form1();

      customControlLogin1.thisFrmObj = this;

}

9.       Run your demo.


Don't want to miss updates? Please click the below button!

Follow MindStick