Saturday, September 29, 2012


In this tutorial I'm going to demonstrate step by step how you can create a custom control in csharp with extended features.
Lets say you want to set border color of a Textbox in your c# windows forms application. The default Textbox control does not provide us the functionality to set its border color , also the Textbox control does not have Paint event so we are not able to draw any border around textbox.
So what we can do is-

  • create a custom control
  • add a textbox in it
  • use custom control's paint event to draw border with any color
So here are the steps to achieve the above goal:

Step 1: First of all open visual studio and create a new project. Do the selection as shown in below snap.



Step 2: Now you get a custom control screen. Resize the control to (153, 22).


Step 3: Right click on Control and select View Code
Step 4: In code file create object of Textbox class and set Multiline to true & BorderStyle to None. Also set font and backcolor of Textbox object equals to custom control & then add the textbox object to custom control as shown below.
Step 5: Create an enum named as BorderSides to give more readable options to user so that the user can select on which side he/she wants to set border (we can also use int 0,1,2,3 and use if else but that is not as easy to understand as enum). Through enum we can assign meaningful names to integers.
Step 6: Now we need to create a public property to get and set textbox's text.
Step 7: Create two public variables to get Border color & Side on which user need to apply border(by default I took All sides) + one private variable for border size



Step 8: Now goto design view & select control >> generate two events(Paint & SizeChanged) of custom control as shown below
Step 9: In CustomTextBox_SizeChanged Set size, location, font and backcolor of textbox according to custom control's size, location, font and backcolor respectively. Note i keep width & height of textbox lil small as compared to custom control just to hold some space to display border.
Step 10: In CustomTextBox_Paint I've used the ControlPaint.DrawBorder to draw border around custom control as shown below. Use enum which we have created earlier to get user choice and set the border accordingly

Step 11: Now from visual studio's menu list Select Built>Built CustomTextBox. After successfull built your custom textbox control dll file will be created in >>Bin>Debug folder

To open project folder from IDE:



Now we have successfully created our custom TextboxControl, To use this control you need to add to it to Toolbox as:
  • Create a new windows form application project
  • Select Toolbox>Right click inside Toolbox and select Choose items
  • Browse the dll file which we have created earlier
  • OK





Now your new control is in your Toolbox's list of all controls, just drag n drop it on  win form to start using it.
Use the Public property Text to set text and public variables-> BorderColor & SetBorderSide to set the border color and side on which the border will be apply respectively


11 comments:

Benjamin Blodgett said...

Very cool tutorial. thanks! :D

Unknown said...

for that i want to set Max Length Property

Anonymous said...

Thank you

Its very clear and useful.

Anonymous said...

Thank you very much.

Anonymous said...

bordercolor error

Anonymous said...

border color not in current context how to solve??????

Kit Westman said...

What sort of modifications would one have to do to build a new control that's a group of controls (like, say, a button paired with a text box)?

Krishan Gahlot said...

@Kit Westman - Just drag drop controls in design view and add properties & functions as per your need and compile to get the dll of your custom control.

ŇÃŜĮŘ ÃββÃŜ said...

Thanks this post
((▄▀▄ Programming Exercises▄▀▄))

Pavithra M said...

It is really a great work and the way in which u r sharing the knowledge is excellent.
Thanks for helping me to understand basic concepts. As a beginner in Dot Net programming your post help me a lot.Thanks for your informative article. Dot Net Training in chennai | Dot Net Training institute in velachery

Mithun Mithun said...

Given so much information in it. its very useful .perfect explanation about Dot net framework.Thanks for your valuable information. Dot Net Training in chennai | Dot Net Training institute in velachery

Post a Comment