Control Class C# Help

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 System, ComponentMode Component class The Component class provides the Control class with the necessary infrastructure that is required to be dropped on a design surface and to be contained y 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 X,Ycoordinates. 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 X,Ycoordinate 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 Apdition 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 if you have to dynamically create for IDs 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 Client Size property is a Size Structure that represents the client area of the control, minus the scroll bar$ and title bar.

The PointToClient and PointToScreen methods are handy conversion methods that take a Point and return a Point. PointToClient takes a Point that represents screen coordinates and translates it to coordinates based on the current client object. This is handy for drag-and-drop actions, 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, or 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. Bysetting the values, you can make the 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) enable.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 BackGround;Image 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~etafile 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,and 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, Click and  DoubleClick receive EventArgs as an argument, whereas the MouseDownand MouseUpevents receive 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 Xand Y coordinates of the mouse. If you have access to any of this information, you will have to handle either the MouseDownor MouseUpevents, 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 KeyPressEventArgs, This contains 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. If you set 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 this, They both receive 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 OR’d 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 OR together, The key events are raised in the following order:

  1. KeyDown
  2. KeyPress
  3. Key up

The Validating, Validated, Enter, Leave, Got Focus, 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, Got Focus, and LostFocus seem to be very similar in what they do, The GotFocus and LostFocus events are lower-level events that are tied to the WM_SETFOCUSand the WM_KILLFCCUSWindows 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 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:

  1. Enter
  2. Got focus
  3. Leave
  4. Vaidating
  5. Validated
  6. Lost focus

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 Systerm 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 safeNativeMethods class, The .NET Framework uses this class to wrap all of the standard Win32API 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 HWl’:JDthat 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.

Togain access to the Windows messages, you can override the WndProc method, The WndProc 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 Functlonality

Some items that are a little more difficult to classify are the data-binding capabilities, The BindingContext property returns a BindingManagerBase object. The DataBindings collection maintains a ControlBindingsCollection, which is a collection of binding objects for the control.

The CompanyName,ProductName, and Product versions provide data on the origination of the control and its current version, The Invalid the 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 sent to the control’s WndProc, 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 the more commonly used ones and is meant to give you an idea of the functionality available.

Posted on November 2, 2015 in Uncategorized, Windows forms

Share the Story

Back to Top