What Do the Component and Control Classes Do for Us?


Visual Basic .NET Unleashed
By Paul Kimmel
Table of Contents
Chapter 16.  Designing User Interfaces

What Do the Component and Control Classes Do for Us?

Frameworks provide us with a hierarchy of relationships. It is beneficial to understand different branches in a framework like the CLR. Understanding the relationships and knowing where the branches are is beneficial to finding opportunistic points to extend a framework into your application.

When it comes to building graphical user interfaces in Visual Basic .NET for Windows applications, the essential first two branches occur at the Component and Control classes. The Component class introduces the notion of visually designable objects. The Control class, which inherits from Component, introduces the notion of a Control that receives messages, can have the input focus, and maintains a Windows handle for these purposes.

Nonvisual components will inherit directly from the Component class, and components that are manipulated visually and have a visual representation on a Windows Form are derived from the Control class.

Component Class

The Component class inherits from MarshalByRefObject, which in turn inherits directly from the root class, Object. Component is the base class for all components in the CLR. Component implements the IComponent interface, which supports the notion of designable objects, and maintains a list of event handlers containing the delegates for a particular component.

IComponent Interface

Component inherits the MarshalByRefObject class. MarshalByRefObject implements the IComponent interface, which allows components to be shared across application boundaries.


Marshaling interapplication communication between objects falls under the auspices of .NET remoting. Basically, Remoting is the capability that replaces DCOM. All of this marshaling messages through object proxies is invisible and automatic while we are designing user interfaces. The first place object marshaling probably occurs is between the assembly that implements the component and Visual Studio .NET designers.

For our purposes we do not need to explore Remoting further, but if you are building distributed applications, the subject of Remoting will be of direct interest to you.

MarshalByRefObject allows messages to be sent across application boundaries using proxies. MarshalByRefObject objects are accessed within the local application domain, as contrasted to MarshalByValueComponent objects, which send a copy of the component to the remote application.


The notion of events supports the Windows operating system's event-driven model. The class EventHandler is a base class for event handlers, and the Component.Events property is an EventHandlerList that maintains a list of events that a particular component will respond to.


The Component.DesignMode property indicates whether the component is currently being manipulated in design mode. This is necessary to support behaviors that are only necessary in the designer, like drawing a focus rectangle around a control that has the focus.

If you want a nonvisual control, you can inherit from the Component class to obtain the minimal capabilities necessary to support design-time manipulation.

Control Class

The Control class represents controls that users interact with directly. The Control class encapsulates the WndProcthe Grand Central Station of Windows messages. You can override the WndProc method to receive any Windows message, but Control classes are capable of handling most Windows messages.

Windows messages are sent to controls because each control has an HWND (window handle) encapsulated by a Handle property, introduced in the Control class.

Because a Control object has a visual presence, Control also makes the canvas (or device context) available through a call to the factory method CreateGraphics.

All of these features combined in the Control class result in controls being able to receive input focus, respond to Windows messages, and support default as well as custom painting.

Finally, Windows Forms controls are not thread-safe but introduce five thread-safe members . The Control class introduces Invoke, InvokeRequired, BeginInvoke, EndInvoke, and CreateGraphics. These thread-safe methods support synchronous and asynchronous control interaction by using delegates to perform work on a control's thread. (Read Chapter 14, "Multithreaded Applications," for more on using these five thread-safe methods .)


Visual BasicR. NET Unleashed
Visual BasicR. NET Unleashed
Year: 2001
Pages: 222

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