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
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