Creating Custom User Controls : Basic – I






1.53/5 (17 votes)
Creating Custom User Controls : Basic – I
Introduction
What Are Controls?
Controls are reusable pieces of user interface functionality. In the Windows world, controls represent the way the user interacts with the application. They allow the user to enter data and manipulate it, to perform certain actions on the application, input data, and display data in a way friendly to the human eye. An application's interface is made up of controls and its functionality is based on the interaction between these controls and the underlying code.
Every software application has an invisible part, which does the actual work, and a visible part, which provides the user interface. Controls are complete software elements that contain both: they are represented on the screen in a graphical way, and they contain code to sustain this interface.
Controls have two major functions:
- To listen to the user's commands and send them to the application
- To display the results from the application in a way that the user will understand
It's interesting to know that the whole idea of the controls has its roots in the development of personal computers. The developers had to emulate some real controls that offered a good look and feel to the application. Let's take the example of a common button. A real button has different forms and sizes and can be of multiple states, can be pushed and may revert if released, or can make a sound when pressed. In order to reproduce the characteristics of a real control many tricks have been used.
Introducing Custom Controls
The term custom is pretty expressive. While in many of your development tasks you can make use only of the default controls provided by your development environment (.NET in our case), in many other cases you'll need to build your own.
Custom-made controls are named, not very surprisingly, custom controls. A custom control is a control designed and programmed by you, and it may make use of other existing controls. Sometimes custom controls are called third-party controls, named by their origin.
The case has been made: creating custom controls can be a necessity when the basic classes provided by the .NET Framework or the ones you can buy from third parties aren't enough, or are too expensive.
The improved coding efficiency you can gain by implementing functionality as a custom control, when it makes sense to do so, can be easily described using an example. If you have to use a pie chart with different elements in it in your application that will present some results in an elegant graphical way, there are two ways to implement this solution.
- 1. You can write the code directly in the form. First, you will have to draw a pie chart with different elements in it, at a certain position. Second, you will have to override the mouse event handler of the form to get events for the chart. Third, assuming that this chart has some functionality, you will have to implement the desired model by attaching the code directly to the form code.
Now if you want to have multiple pie charts in an application, you need to follow the three steps mentioned above for each of them. Afterwards, even changing some simple functionality, such as moving a certain action from left button to right button, will need to be done three times. Your code will contain lots of duplicate functionality and will be hard to read, understand, debug, and extend. Not to mention that every time you modify the chart, you will have to rebuild your entire application.
- You can build a custom control. You will create a pie chart custom control that draws itself and has its own events and event handler mechanisms. It will expose different properties and methods necessary in the form. This custom control's position can then be easily changed inside the forms that use it by simply setting its coordinates. Also, once this custom control is created you will gain precious time, because the time you will spend making changes, adding extra features, and debugging the custom control will be shorter and code modification will happen in one place—the control code.
Packing functionality in the form of user controls brings a number of important benefits:
- Building custom controls facilitates code reusability because the same control can be used in any number of forms or tabs (or even other custom controls), without having to write the same code over and over again. This saves a lot of time in application development and untangles application code.
- It encourages functionality re-usability, under OOP's "black box" principle. You don't need to know how the control works inside; all you need to know is the public interface it exposes. For example, think about one of the simplest controls available: the Label control. When working with labels in a Windows Forms project, you know that you need to set the label's Text property to the text you want displayed. You never care how the label works internally, and how it actually paints that text on the screen (it may not be obvious at the first sight, but work needs to be done even for such a simple task as painting some text on the form). Extrapolating from this simple example, you can get a feeling about how the black box concept applies to the more complex controls.
- It keeps application code simple. Let's say you need that your application, among other things, knows how to play sounds. Using a custom control to implement the functionality for playing sounds minimizes the code written in the application form. Instead of creating buttons and components, and adding and handling their events in the application code, you can simply create a custom control that implements this functionality, and exposes it through a public interface that the application can use. Using custom controls keeps application code simple because the functionality is implemented inside the control and not in the application's form. In the extreme case, a form could be built exclusively of controls that are interacting with each other, and have no functionality implemented in it.
- Custom controls can be developed, compiled, packaged, and even sold separately, just like regular applications. This gives you a lot of flexibility in the way you develop and then use the controls.
- Building custom controls can make it easier to improve the appearance and usability of your application by implementing user-friendly code and design into the controls. If you want your application to look a certain way, setting the .NET Framework's controls' appearance properties isn't enough to create it. By creating custom controls with the appearance you want, your can greatly improve your application's look, feel, and functionality. This can be a fairly easy way to win more happy users on your side, because the user interface created specifically for the application can be much more user-friendly than one built with the built-in .NET controls.