|
This course
- About Custom Controls
last updated: 08/07/2006 |
What are custom controls?
A control is a reusable component such as a textbox, label, or drop-down list. Many
standard controls come built into .NET, and you can inherit from ("extend") these
controls to accomplish many useful things.
Why would you use one?
|
It is sometimes useful to create each instance of a control with identifying information. For example, if you have a grid of many buttons where each button, when clicked, calls the same event handler in the form that contains the buttons--and the form then needs the button to tell it which button was clicked.
This can be done with a customized button, where you pass the button's
identifying information to the button constructor (such as what row and
column it's in). When the button is clicked, the form's event handler
can ask the button which row and column it's in. The code additions to
the button are minimal--pass in a couple of arguments to the constructor,
and provide a property (getter and setter) to cough up the passed-in
arguments to the form later. |
|
Suppose I want all my buttons to be light blue, but turn yellow when your mouse is over it. Instead of setting these properties individually on each button and have duplicate code in each button handler, I could use a custom control and add this behavior to a specialized "custom button". Then, all the custom buttons would automatically behave this way. As with any control, I could even reuse this custom control in many different projects. |
Two ways of using custom controls
|
This is for visual, manual placement on a form by dragging and resizing. For this case, these are the basic steps:
|
|
This is typically used for the information storage type. If you have to send individual information to each control when it's instantiated, you'll have to write code to do it. For this case, these are the basic steps:
|