15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Control Class ❘ 1123<br />

the Control class. Later in the chapter, when you look at the specific controls based on the Control class,<br />

you will see the properties <strong>and</strong> methods in some sample code. The following subsections group the methods<br />

<strong>and</strong> properties by functionality, so related items can be looked at together.<br />

size <strong>and</strong> location<br />

The size <strong>and</strong> location of a control are determined by the properties Height, Width, Top, Bottom, Left,<br />

<strong>and</strong> Right along with the complementary properties Size <strong>and</strong> Location. The difference is that Height,<br />

Width, Top, Bottom, Left, <strong>and</strong> Right all take single integers as their value. Size takes a Size structure<br />

<strong>and</strong> Location takes a Point structure as their values. The Size <strong>and</strong> Point structures are a contained<br />

version of X,Y coordinates. Point generally relates to a location <strong>and</strong> Size is the height <strong>and</strong> width of an<br />

object. Size <strong>and</strong> Point are in the System.Drawing namespace. Both are very similar in that they provide<br />

an X,Y coordinate pair but also have overridden operators for easy comparison <strong>and</strong> conversion. You can,<br />

for example, add two Size structures together. In the case of the Point structure, the Addition operator<br />

is overridden so that you can add a Size structure to a Point <strong>and</strong> get a new Point in return. This has<br />

the effect of adding distance to a location <strong>and</strong> getting a new location. This is very h<strong>and</strong>y if you have to<br />

dynamically create forms or controls.<br />

The Bounds property returns a Rectangle object that represents the area of a control. This area includes<br />

scroll bars <strong>and</strong> title bars. Rectangle is also part of the System.Drawing namespace. The ClientSize<br />

property is a Size structure that represents the client area of the control, minus the scroll bars <strong>and</strong> title bar.<br />

The PointToClient <strong>and</strong> PointToScreen methods are h<strong>and</strong>y conversion methods that take a Point <strong>and</strong><br />

return a Point. PointToClient takes a Point that represents screen coordinates <strong>and</strong> translates it to<br />

coordinates based on the current client object. This is h<strong>and</strong>y for drag-<strong>and</strong>-drop actions. PointToScreen<br />

does just the opposite — it takes coordinates of a client object <strong>and</strong> translates them to screen coordinates.<br />

The RectangleToScreen <strong>and</strong> ScreenToRectangle methods perform the same functionality with<br />

Rectangle structures instead of Points.<br />

The Dock property determines which edge of the parent control the control will be docked to. A DockStyle<br />

enumeration value is used as the property’s value. This value can be Top, Bottom, Right, Left, Fill, or<br />

None. Fill sets the control’s size to match the client area of the parent control.<br />

The Anchor property anchors an edge of the control to the edge of the parent control. This is different from<br />

docking in that it does not set the edge to the parent control but sets the current distance from the edge so<br />

that it is constant. For example, if you anchor the right edge of the control to the right edge of the parent<br />

<strong>and</strong> the parent is resized, the right edge of the control will maintain the same distance from the parent’s<br />

right edge. The Anchor property takes a value of the AnchorStyles enumeration. The values are Top,<br />

Bottom, Left, Right, <strong>and</strong> None. By setting the values, you can make the control resize dynamically with the<br />

parent as the parent is resized. This way, buttons <strong>and</strong> text boxes will not be cut off or hidden as the form is<br />

resized by the user.<br />

The Dock <strong>and</strong> Anchor properties used in conjunction with the Flow <strong>and</strong> Table layout controls (discussed<br />

later in this chapter) enable you to create very sophisticated user windows. Window resizing can be difficult<br />

with complex forms with many controls. These tools help make that process much easier.<br />

appearance<br />

Properties that relate to the appearance of the control are BackColor <strong>and</strong> ForeColor, which take a<br />

System.Drawing.Color object as a value. The BackGroundImage property takes an Image-based object as<br />

a value. The System.Drawing.Image class is an abstract class that is used as the base for the Bitmap <strong>and</strong><br />

Metafile classes. The BackgroundImageLayout property uses the ImageLayout enumeration to set how<br />

the image is displayed on the control. Valid values are Center, Tile, Stretch, Zoom, <strong>and</strong> None.<br />

The Font <strong>and</strong> Text properties deal with displaying the written word. In order to change the Font you will<br />

need to create a Font object. When you create the Font object, you specify the font name, size, <strong>and</strong> style.<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!