You’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.
These 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.
This 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.
The 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.
This 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.
This 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.
This 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.
Bringing Selected Controls into View
In addition to the Autoscroll properties, the Form object provides the Scroll method, which allows you to scroll a form programmatically, and ScrollControlIntoView, which scrolls the form until the specified control comes into view. The Scroll method accepts as arguments the horizontal and vertical displacements of the scrolling operation, whereas ScrollControlIntoView accepts as an argument the control you want to bring into view. Notice that activating a control with the Tab key automatically brings the control into view if it’s not already visible on the form. Finally, the Scroll event is fired every time a form is scrolled.
The 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
|None||A borderless window that can't be resized. This setting is rarely used.|
|Sizable||(default) A resizable window that's used for displaying regular forms.|
|Fixed3D||A window with a fixed visible border, "raised" relative to the main area. Unlike the None setting, this setting allows users to minimize and close the window.|
|FixedDialog||A fixed window used to implement dialog boxes.|
|FixedSingle||A fixed window with a single-line border.|
|FixedToolWindow||A fixed window with a Close button only. It looks like a toolbar displayed by drawing and imaging applications.|
|SizableToolWindow||Same as the FixedToolWindow, but is resizable. In addition, its caption font is smaller than the usual.|
This 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.
These 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.
These 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:
Me.MinimumSize = New Size(400, 300)
Or you can set the width and height separately:
Me.MinimumSize.Width = 400
Me.MinimumSize.Height = 300
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:
Me.MinimumSize = New Size(400, 300 + SystemInformation.CaptionHeight)
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.
Use the SystemInformation Class to Read System Information
The height of the caption is not a property of the Form object, even though it’s used to determine the useful area of the form (the total height minus the caption bar). Keep in mind that the height of the caption bar is given by the CaptionHeight property of the SystemInformation object. You should look up the SystemInformation object, which exposes a lot of useful properties — such as BorderSize (the size of the form’s borders), Border3DSize (the size of three-dimensional borders), CursorSize (the cursor’s size), and many more.
This 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. For more information on processing keystrokes at the form level and using special keystrokes throughout your application, see the Contacts project later in this chapter.
This 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).
The 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: CenterParent (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.
This 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.
Use 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.