Visual Basic.NET 2008 > Working with Forms VB Forms - Forms in Visual Basic.NET 2008
The Appearance of Forms
Applications are made up of one or more forms — usually more than one. You should craft your forms carefully, make them functional, and keep them simple and intuitive. You already know how to place controls on the form, but there's more to designing forms than populating them with controls. The main characteristic of a form is the title bar on which the form's caption is displayed (see Figure 5.1). Clicking the icon on the left end of the title bar opens the Control menu, which contains the commands shown in Table 5.1. On the right end of the title bar are three buttons: Minimize, Maximize, and Close. Clicking these buttons performs the associated function. When a form is maximized, the Maximize button is replaced by the Restore button. When clicked, the Restore button resets the form to the size and position before it was maximized, and it's replaced by the Maximize button. To access the Control menu without a mouse, press Alt and then the down arrow key.
Table 5.1 - Commands of the Control Menu of the Form
Properties of the Form ObjectYou're familiar with the appearance of forms, even if you haven't programmed in the Windows environment in the past; you have seen nearly all types of windows in the applications you're using every day. The floating toolbars used by many graphics applications, for example, are actually forms with a narrow title bar. The dialog boxes that display critical information or prompt you to select the file to be opened are also forms. You can duplicate the look of any window or dialog box through the following properties of the Form object. AcceptButton, CancelButtonThese two properties let you specify the default Accept and Cancel buttons. The Accept button is the one that's automatically activated when you press Enter, no matter which control has the focus at the time, and is usually the button with the OK caption. Likewise, the Cancel button is the one that's automatically activated when you hit the Esc key and is usually the button with the Cancel caption. To specify the Accept and Cancel buttons on a form, locate the AcceptButton and CancelButton properties of the form and select the corresponding controls from a drop-down list, which contains the names of all the buttons on the form. For more information on these two properties, see the section "Forms versus Dialog Boxes in VB.NET," later in this chapter. AutoScaleModeThis property determines how the control is scaled, and its value is a member of the AutoScale-Mode enumeration: None (automatic scaling is disabled), Font (the controls on the form are scaled relative to the size of their font), Dpi, which stands for dots per inch (the controls on the form are scaled relative to the display resolution), and Inherit (the controls are scaled according to the AutoScaleMode property of their parent class). The default value is Font; if you change the form's font size, the controls on it are scaled to the new font size. AutoScrollThe AutoScroll property is a True/False value that indicates whether scroll bars will be automatically attached to the form if the form is resized to a point that not all its controls are visible. Use this property to design large forms without having to worry about the resolution of the monitor on which they'll be displayed. The AutoScroll property is used in conjunction with two other properties (described a little later in this section): AutoScrollMargin and AutoScrollMinSize. Note that the AutoScroll property applies to a few controls as well, including the Panel and SplitContainer controls. For example, you can create a form with a fixed and a scrolling pane by placing two Panel controls on it and setting the AutoScroll property of one of them (the Panel you want to scroll) to True. AutoScrollPositionThis property is available from within your code only (you can't set this property at design time), and it indicates the number of pixels that the form was scrolled up or down. Its initial value is zero, and it assumes a value when the user scrolls the form (provided that the form's AutoScroll property is True). Use this property to find out the visible controls from within your code, or scroll the form programmatically to bring a specific control into view. AutoScrollMarginThis is a margin, expressed in pixels, that's added around all the controls on the form. If the form is smaller than the rectangle that encloses all the controls adjusted by the margin, the appropriate scroll bar(s) will be displayed automatically. AutoScrollMinSizeThis property lets you specify the minimum size of the form before the scroll bars are attached. If your form contains graphics that you want to be visible at all times, set the Width and Height members of the AutoScrollMinSize property to the dimensions of the graphics. (Of course, the graphics won't be visible at all times, but the scroll bars indicate that there's more to the form than can fit in the current window.) Notice that this isn't the form's minimum size; users can make the form even smaller. To specify a minimum size for the form, use the MinimumSize property, described later in this section. Let's say the AutoScrollMargin property of the form is 180 × 150. If the form is resized to fewer than 180 pixels horizontally or 150 pixels vertically, the appropriate scroll bars will appear automatically, as long as the AutoScroll property is True. If you want to enable the Auto-Scroll feature when the form's width is reduced to anything fewer than 250 pixels, set the AutoScrollMinSize property to (250, 0). In this example, setting AutoScrollMinSize.Width to anything less than 180, or AutoScrollMinSize.Height to anything less than 150, will have no effect on the appearance of the form and its scroll bars.
FormBorderStyleThe FormBorderStyle property determines the style of the form's border; its value is one of the FormBorderStyle enumeration's members, which are shown in Table 5.2. You can make the form's title bar disappear altogether by setting the form's FormBorderStyle property to FixedToolWindow, the ControlBox property to False, and the Text property (the form's caption) to an empty string. However, a form like this can't be moved around with the mouse and will probably frustrate users. Tabel 5.2 - The FormBorderStyle Enumeration
ControlBoxThis property is also True by default. Set it to False to hide the control box icon and disable the Control menu. Although the Control menu is rarely used, Windows applications don't disable it. When the ControlBox property is False, the three buttons on the title bar are also disabled. If you set the Text property to an empty string, the title bar disappears altogether. MinimizeBox, MaximizeBoxThese two properties, which specify whether the Minimize and Maximize buttons will appear on the form's title bar, are True by default. Set them to False to hide the corresponding buttons on the form's title bar. MinimumSize, MaximumSizeThese two properties read or set the minimum and maximum size of a form. When users resize the form at runtime, the form won't become any smaller than the dimensions specified by the MinimumSize property and no larger than the dimensions specified by the MaximumSize property. The MinimumSize property is a Size object, and you can set it with a statement like the following:
Or you can set the width and height separately:
The MinimumSize.Height property includes the height of the form's title bar; you should take that into consideration. If the minimum usable size of the form is 400 × 300, use the following statement to set the MinimumSize property:
The default value of both properties is (0, 0), which means that no minimum or maximum size is imposed on the form, and the user can resize it as desired.
KeyPreviewThis property enables the form to capture all keystrokes before they're passed to the control that has the focus. Normally, when you press a key, the KeyPress event of the control with the focus is triggered (as well as the KeyUp and KeyDown events), and you can handle the keystroke from within the control's appropriate handler. In most cases, you let the control handle the keystroke and don't write any form code for that. If you want to use "universal" keystrokes in your application, you must set the KeyPreview property to True. Doing so enables the form to intercept all keystrokes, so you can process them from within the form's keystroke event handlers. To handle a specific keystroke at the form's level, set the form's KeyPreview property to True and insert the appropriate code in the form's KeyDown or KeyUp event handler (the KeyPress event isn't fired for the function keys). The same keystrokes are then passed to the control with the focus, unless you "kill" the keystroke by setting its SuppressKeystroke property to True when you process it on the form's level. Formore information on processing keystrokes at the form level and using special keystrokes throughout your application, see the Contacts project later in this chapter. SizeGripStyleThis property gets or sets the style of the sizing handle to display in the bottom-right corner of the form. You can set it to a member of the SizeGripStyle enumeration: Auto (the size grip is displayed as needed), Show (the size grip is displayed at all times), or Hide (the size grip is not displayed, but users can still resize the form with the mouse). StartPosition, LocationThe StartPosition property, which determines the initial position of the form when it's first displayed, can be set to one of the members of the FormStartPosition enumeration: Center-Parent (the form is centered in the area of its parent form), CenterScreen (the form is centered on the monitor), Manual (the position of the form is determined by the Location property), WindowsDefaultLocation (the form is positioned at the Windows default location), and WindowsDefaultBound (the form's location and bounds are determined by Windows defaults). The Location property allows you to set the form's initial position at design time or to change the form's location at runtime. TopMostThis property is a True/False value that lets you specify whether the form will remain on top of all other forms in your application. Its default property is False, and you should change it only on rare occasions. Some dialog boxes, such as the Find & Replace dialog box of any text-processing application, are always visible, even when they don't have the focus. For more information on using the TopMost property, see the discussion of the TextPad project in Chapter, " Windows Controls in VB.NET" You can also add a professional touch to your application by providing a CheckBox control that determines whether a form should remain on top of all other forms of the application. SizeUse the Size property to set the form's size at design time or at runtime. Normally, the form's width and height are controlled by the user at runtime. This property is usually set from within the form's Resize event handler to maintain a reasonable aspect ratio when the user resizes the form. The Form object also exposes the Width and Height properties for controlling its size.
Table of Contents
|
|||||||||||||||||||||||||||||||||||
W3computing.com Copyright 2011 © All Rights Reserved
|
|||||||||||||||||||||||||||||||||||
| Home | Useful links | Contact us | |||||||||||||||||||||||||||||||||||