Using the Control and UserControl Classes


You can find information about the Control and UserControl classes of the System.Windows.Forms namespace in the Reference/Class Library section of the .NET Framework Software Development Kit (SDK) documentation. You should be familiar with these two classes before you start coding your Windows custom control. You should, for example, know what methods and properties are available so that you do not end up writing your own while you could have used the existing ones. This section addresses some important points about the Control and UserControl classes.

The Control class is the central member of the System.Windows.Forms namespace. It is the direct or indirect parent class of Windows controls. The Form class, for example, also derives from the Control class. The Control class has properties, methods, and events that make programming Windows controls easy.

For starters, the Control class has more than 70 properties. There are properties that deal with its appearance and dimension (BackColor, ForeColor, Font, Visible, Width, Size, Location, Left, Top, Dock), properties to manage other controls as child controls (HasChildren, Controls), a property that enables the control to reference its container (Parent), and so on.

It also has useful methods you use in child classes, such as Focus, Hide, Dispose, GetNextControl, and Show. Additionally, it has useful methods that you only use when creating your own controls by extending the Control class, such as Invalidate, Update, ProcessDialogKey, ProcessDialogChar, and so on.

Last but not least, there are events that trigger automatically when an action is carried out upon that control: Click, DoubleClick, MouseOver, MouseDown, MouseUp, Resize, and so on. The Paint event triggers whenever the control needs to be repainted.

Note

In the .NET Framework class library, events in a class are triggered by the OnXXX methods in that class. For example, the OnClick method raises the Click event. The OnPaint method triggers the Paint event, and so on. You can override these OnXXX methods to change their behaviors in sub classes. You will learn about this more in the "Creating Events with Delegates and the Observer Design Pattern" section. For now, you will override the OnPaint method of the Control class to provide a GUI for your control.

The UserControl class derives directly from the Control class and provides several additional members. You will extend this class in the StyledTextArea control. When extending the Control or UserControl classes, you are responsible for providing and managing your own GUI. This brings us to the next topic, managing the GUI.

Tip

When creating a custom Windows control, you do not always extend the Control or UserControl class directly. In many circumstances, you may want to extend other control classes. For instance, to create a text box that accepts numbers only, it is easier to extend the TextBox class. Also, even when you extend the Control or UserControl class, you still can incorporate other controls in your custom control.




Real World. NET Applications
Real-World .NET Applications
ISBN: 1590590821
EAN: 2147483647
Year: 2005
Pages: 82

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net