Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


Chapter 4
Managing Forms In Visual Basic

If you need an immediate solution to:
Setting Title Bar Text
Adding/Removing Min/Max Buttons And Setting A Window’s Border
Adding Toolbars To Forms
Adding Status Bars To Forms
Referring To The Current Form
Redrawing Form Contents
Setting Control Tab Order
Moving And Sizing Controls From Code
Showing And Hiding Controls In A Form
Measurements In Forms
Working With Multiple Forms
Loading, Showing, And Hiding Forms
Setting The Startup Form
Creating Forms In Code
Using The Multiple Document Interface
Arranging MDI Child Windows
Opening New MDI Child Windows
Arrays Of Forms
Coordinating Data Between MDI Child Forms (Document Views)
Creating Dialog Boxes
All About Message Boxes And Input Boxes
Passing Forms To Procedures
Minimizing/Maximizing And Enabling/Disabling Forms From Code

In Depth

In this chapter, we’ll take a look at handling forms in Visual Basic. There’s a great deal to see about form handling, and we’ll look at it all. We’ll see how to customize forms, how to work with multiple forms, how to support the multiple document interface (MDI), how to coordinate MDI child forms, how to use the MsgBox() and InputBox() functions, how to load, hide, show, and unload forms, and much more. We’ll begin the chapter by getting an overview of Visual Basic forms.

The Parts Of A Form

Forms are the names for windows in Visual Basic (originally, you called windows under design forms, and the actual result when running a window, but common usage has named both forms now), and you add controls to forms in the Integrated Development Environment (IDE).

We’re designing a form in the Visual Basic IDE in Figure 4.1, and you can see several aspects of forms there. At the top of the form is the title bar, which displays the form’s title; here that’s just Form1. At right in the title bar is the control box, including the minimizing/maximizing buttons and the close button. These are controls the user takes for granted in most windows, although we’ll see they are inappropriate in others (such as dialog boxes).


Figure 4.1  A form under design.

Under the title bar comes the menu bar, if there is one. In Figure 4.1, the form has one menu: the File menu (we’ll see how to work with menus in the next chapter). Under the menu bar, forms can have toolbars, as you see in the IDE itself.

The main area of a form—the area where everything takes place—is called the client area. In general, Visual Basic code works with controls in the client area and leaves the rest of the form to Visual Basic (in fact, the client area is itself a window). In Figure 4.1, we’ve added a control—a command button—to the form.

Finally, the whole form is surrounded by a border, and there are several types of borders that you can use.

The Parts Of An MDI Form

Besides standard forms, Visual Basic also supports MDI forms. An MDI form appears in Figure 4.2.


Figure 4.2  An MDI form.

You can see that an MDI form looks much like a standard form, with one major difference, of course—the client area of an MDI form acts like a kind of corral for other forms. That is, an MDI form can display MDI child forms in it, which is how the multiple document interface works. In Figure 4.2, we have two documents open in the MDI form.

That’s the third type of form you can have in Visual Basic—MDI child forms. These forms appear in MDI child windows, but otherwise are very similar to standard forms.

Those, then, are the three types of forms available to us in Visual Basic: standard forms, MDI forms, and MDI child forms. We’ll work with all of them in this chapter. In fact, we’re ready to start getting into the details now as we turn to the Immediate Solutions section of this chapter.

Immediate Solutions

Setting Title Bar Text

You’ve submitted your project to the user-testing stage and feel smug. What could go wrong? Suddenly the phone rings—seems they don’t like the title in the program’s title bar: “Project1”. How can you change it?

This stymies a lot of Visual Basic programmers, because the text in the title bar seems like something that Windows itself manages, not the program. In fact, it’s up to the program, and setting the text in the title bar couldn’t be easier. At design time, you just change the form’s Caption property, as shown in Figure 4.3.


Figure 4.3  Setting a form’s caption.

You can also set the Caption property at runtime in code like this (note that we use the Me keyword here to refer to the current form—see “Referring to the Current Form” later in this chapter):

Private Sub Command1_Click()
    Me.Caption = "Hello from Visual Basic!"
End Sub

Adding/Removing Min/Max Buttons And Setting A Window’s Border

Forms usually come with minimizing and maximizing buttons, as well as a close box at the upper right. However, that’s not appropriate in all cases, as we’ll see when we design dialog boxes later in this chapter.

To remove these buttons, you can set the form’s ControlBox property to False, as shown in Figure 4.4. Note that the usual buttons are missing from the form at the upper right.


Figure 4.4  Removing the control box from a form.


TIP:  If you are thinking of designing a dialog box, take a look at “Creating Dialog Boxes” later in this chapter—besides removing the control box, you should also set the dialog’s border correctly, add OK and Cancel buttons, and take care of a few more considerations.

You can also set what buttons are in a form by setting its border type. For example, if you set the border style to a fixed type, the minimizing and maximizing buttons will disappear.

Setting A Form’s Border

You set a form’s border style with its BorderStyle property; here are the possible values for that property:

  0—None
  1—Fixed Single
  2—Sizable
  3—Fixed Dialog
  4—Fixed Tool window
  5—Sizable Tool window

We’ll see more about using the BorderStyle property when we work with dialog boxes in this chapter.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.