Control Class


The System.Windows.Forms namespace has one particular class that is the base class for virtually every control and form that is created. This class is the System.Windows.Forms.Control class. The Control class implements the core functionality to create the display that the user sees. The Control class is derived from the System.ComponentModel.Component class. The Component class provides the Control class with the necessary infrastructure required to be dropped on a design surface and to be contained by another object. The Control class provides a large list of functionality to the classes that are derived from it. The list is too long to itemize here, so this section looks at the more important items that are provided by the Control class. Later in the chapter when you look at the specific controls based on the Control class you will see the properties and methods in some example code. The following subsections group the methods and properties by functionality, so related items can be looked at together.

Size and Location

The size and location of a control are determined by the properties Height, Width, Top, Bottom, Left, and Right along with the complementary properties Size and Location. The difference is that Height, Width, Top, Bottom, Left, and Right all take single integers as their value. Size takes a Size structure and Location takes a Point structure as their values. The Size and Point structures are a contained version of XY coordinates. Point generally relates to a location and Size is the height and width of an object. Size and Point are in the System.Drawing namespace. Both are very similar in that they provide an XY coordinate pair but also have overridden operators for easy comparison and conversion. You can, for example, add two Size structures together. In the case of the Point structure the Addition operator is overridden so that you can add a Size structure to a Point and get a new Point in return. This has the effect of adding distance to a location and getting a new location. This is very handy ifyou're in the situation of having to dynamically create forms or controls.

The Bounds property returns a Rectangle object that represents the area of a control. This area includes scroll bars and title bars. Rectangle is also part of the System.Drawing namespace. The ClientSize property is a Size structure that represents the client area of the control, minus the scroll bars and title bar.

The PointToClient and PointToScreen methods are handy conversion methods that take a Point and return a Point. The PointToClient takes a Point that represents screen coordinates and translates them to coordinates based on the current client object. This is handy for drag-and-drop actions. The PointToScreen does just the opposite — it takes coordinates of a client object and translates them to screen coordinates. The RectangleToScreen and ScreenToRectangle methods perform the same functionality with Rectangle structures instead of Points.

The Dock property determines which edge of the parent control the control will be docked to. A DockStyle enumeration value is used as the property's value. This value can be Top, Bottom, Right, Left, Fill, and None. Fill sets the control's size to match the client area of the parent control.

The Anchor property anchors an edge of the control to the edge of the parent control. This is different from docking in that it does not set the edge to the parent control, but sets the current distance from the edge to be constant. For example, if you anchor the right edge of the control to the right edge of the parent and the parent is resized, the right edge of the control will maintain the same distance from the parent's right edge. The Anchor property takes a value of the AnchorStyles enumeration. The values are Top, Bottom, Left, Right, and None. By setting the values you can make control resize dynamically with the parent as the parent is resized. This way, buttons and text boxes will not be cut off or hidden as the form is resized by the user.

The Dock and Anchor properties used in conjunction with the Flow and Table layout controls (discussed later in this chapter) allow you to create very sophisticated user windows. Window resizing can be difficult with complex forms with many controls. These tools help make that process much easier.

Appearance

Properties that relate to the appearance of the control are BackColor and ForeColor, which take a System.Drawing.Color object as a value. The BackGroundImage property takes an Image-based object as a value. The System.Drawing.Image class is an abstract class that is used as the base for the Bitmap and Metafile classes. The BackgroundImageLayout property uses the ImageLayout enumeration to set how the image is displayed on the control. Valid values are Center, Tile, Stretch, Zoom, or None.

The Font and Text properties deal with displaying the written word. In order to change the Font you will need to create a Font object. When you create the Font object you specify the font name, size, and style.

User Interaction

User interaction is best described as the various events that a control creates and responds to. Some of the more common events are Click, DoubleClick, KeyDown, KeyPress, Validating, and Paint.

The Mouse events — Click, DoubleClick, MouseDown, MouseUp, MouseEnter, MouseLeave, and MouseHover — deal with the interaction of the mouse and the control. If you are handling both the Click and the DoubleClick events, every time you catch a DoubleClick event the Click event is raised as well. This can result in undesired results if not handled properly. Also the Click and DoubleClick receive an EventArgs as an argument, whereas the MouseDown and MouseUp events receive a MouseEventArgs. The MouseEventArgs contain several pieces of useful information such as the button that was clicked, the number of times the button was clicked, the number of mouse wheel detents (notches in the mouse wheel), and the current X and Y coordinates of the mouse. If you have access to any of this information, you will have to handle either the MouseDown or MouseUp events, not the Click or DoubleClick events.

The keyboard events work in a similar fashion: The amount of information needed determines the event that is handled. For simple situations the KeyPress event receives a KeyPressEventArgs. This contains the KeyChar, which is a char value that represents the key pressed. The Handled property is used to determine whether or not the event was handled. By setting the Handled property to true, the event is not passed on for default handling by the operating system. If you need more information about the key that was pressed, the KeyDown or KeyUp event is more appropriate to handle. They both receive a KeyEventArgs. Properties in KeyEventArgs include whether the Ctrl, Alt, or Shift key was pressed. The KeyCode property returns a Keys enumeration value that identifies the key that was pressed. Unlike the KeyPressEventArgs.KeyChar property, the KeyCode property tells you about every key on the keyboard, not just the alphanumeric keys. The KeyData property returns a Keys value and will also set the modifier. The modifiers are ORd with the value. This tells you that the Shift key or the Ctrl key was pressed as well. The KeyValue property is the int value of the Keys enumeration. The Modifiers property contains a Keys value that represents the modifier keys that were pressed. If more than one has been selected, the values are ORd together. The key events are raised in the following order:

  1. KeyDown

  2. KeyPress

  3. KeyUp

The Validating, Validated, Enter, Leave, GotFocus, and LostFocus events all deal with a control gaining focus (or becoming active) or losing focus. This happens when the user tabs into a control or selects the control with the mouse. Enter, Leave, GotFocus, and LostFocus seem to be very similar in what they do. The GotFocus and LostFocus events are lower-level events that are tied to theWM_SETFOCUS and the WM_KILLFOCUS Windows messages. Generally you should use the Enter and Leave events if possible. The Validating and Validated events are raised when the control is validating. These events receive a CancelEventArgs. With this you can cancel the following events by setting the Cancel property to true. If you have custom validation code, and validation fails, you can set Cancel to true and the control will not lose focus. Validating occurs during validation; Validated occurs after validation. The order in which these events are raised is as follows:

  1. Enter

  2. GotFocus

  3. Leave

  4. Validating

  5. Validated

  6. LostFocus

Understanding the order of these events is important so that you don't inadvertently create a recursive situation. For example, trying to set the focus of a control from the control's LostFocus event creates a message deadlock and the application stops responding.

Windows Functionality

The System.Windows.Forms namespace is one of the few namespaces that relies on Windows functionality. The Control class is a good example of that. If you were to do a disassembly of the System. Windows.Forms.dll, you would see a list of references to the UnsafeNativeMethods class. The .NET Framework uses this class to wrap all of the standard Win32 API calls. By using interop to the Win32 API, the look and feel of a standard Windows application can still be achieved with the System. Windows.Forms namespace.

Functionality that supports the interaction with Windows includes the Handle and IsHandleCreated properties. Handle returns an IntPtr that contains the HWND (windows handle) for the control. The window handle is an HWND that uniquely identifies the window. A control can be considered a window, so it has a corresponding HWND. You can use the Handle property to call any number of Win32 API calls.

To gain access to the windows messages you can override the WndProc method. The WindProc method takes a Message object as a parameter. The Message object is a simple wrapper for a windows message. It contains the HWnd, LParam, WParam, Msg, and Result properties. If you want to have the message processed by the system, you must make sure that you pass the message to the base.WndProc(msg) method. If you want to handle the message, you don't want to pass the message on.

Miscellaneous Functionality

Some items that are a little more difficult to classify are the data-binding capabilities. The Binding Context property returns a BindingManagerBase object. The DataBindings collection maintains a ControlBindingsCollection, which is a collection of binding objects for the control. Data-binding is discussed in Chapter 24, "Viewing .NET Data."

The CompanyName, ProductName, and Product versions provide data on the origination of the control and its current version.

The Invalidate method allows you to invalidate a region of the control for repainting. You can invalidate the entire control or specify a region or rectangle to invalidate. This causes a paint message to be sent to the control's WindProc. You also have the option to invalidate any child controls at the same time.

Dozens of other properties, methods, and events make up the Control class. This list represents some of the more commonly used ones and is meant to give you an idea of the functionality available.




Professional C# 2005
Pro Visual C++ 2005 for C# Developers
ISBN: 1590596080
EAN: 2147483647
Year: 2005
Pages: 351
Authors: Dean C. Wills

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