Visual Basic.NET 2008 > Working with Forms

VB Forms - Forms in Visual Basic.NET 2008

<< Previous Page Next Page >>

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.

VB.NET _ The elements of the form

Figure 5.1 - The elements of the form

Table 5.1 - Commands of the Control Menu of the Form

Command Effect
Restore Restores a maximized form to the size it was before it was maximized; available only if the form has been maximized.
Move Lets the user move the form around with the arrow keys.
Size Lets the user resize the form with the arrow keys.
Minimize Minimizes the form.
Maximize Maximizes the form.
Close Closes the current form. (Closing the application's main form terminates the application.)

 

Properties of the Form Object

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.

AcceptButton, CancelButton

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.

AutoScaleMode

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.

AutoScroll

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.

AutoScrollPosition

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.

AutoScrollMargin

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.

AutoScrollMinSize

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.

FormBorderStyle

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

Value Effect
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.

 

ControlBox

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.

MinimizeBox, MaximizeBox

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.

MinimumSize, MaximumSize

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.

KeyPreview

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. Formore information on processing keystrokes at the form level and using special keystrokes throughout your application, see the Contacts project later in this chapter.

SizeGripStyle

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).

StartPosition, Location

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: 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.

TopMost

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.

Size

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.

<< Previous Page Next Page >>

Table of Contents

  1. Working with Forms in Visual Basic.NET
  2. VB Forms - Forms in Visual Basic.NET - The Appearance of Forms
    1. Properties of the Form Object in VB.NET
    2. Placing Controls on Forms in VB.NET
    3. Setting the TabIndex Property of the controls in a Form in Visual Basic.NET
    4. An Example Contacts Project using Forms in Visual Basic.NET
  3. Anchoring and Docking of Control in a Form in Visual Basic.NET
  4. Splitting Forms into Multiple Panes - Visual Basic.NET
  5. The Form’s Events - Visual basic.NET
  6. Loading and Showing Forms - Visual Basic.NET
    1. The Startup Form - Visual Basic.NET
    2. Controlling One Form from within Another - Visual Basic.NET
  7. Forms versus Dialog Boxes - Visual Basic.NET
    1. The MultipleForms Example - Forms in VB.NET 2008
  8. Building Dynamic Forms at Runtime - VB.NET 2008
    1. The Form’s Controls Collection - Visual Basic 2008
    2. Creating Event Handlers at Runtime - Forms in Visual Basic.NET
  9. Designing Menus, The Menu Editor in Visual Basic.NET
  10. The ToolStripMenuItem Properties - Visual Basic.NET
  11. Manipulating Menus at Runtime - VB.NET 2008
  12. Iterating a Menu’s Items - Vb.NET 2008





     
 
W3computing.com Copyright 2011 All Rights Reserved
 
  Home | Useful links | Contact us