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


Creating Forms In Code

You’ve added a handy calculator form to your financial planning program—but you find that many users have several calculations open at once and want to open multiple calculators. How do you create and display new forms like that in Visual Basic?

New forms are simply new objects in Visual Basic. To declare a new form based on a form you already have, say Form1, you just use Dim :

Private Sub NewForm_Click()
    Dim NewForm As Form1
...
End Sub

Next, you create the new form with the New keyword:

Private Sub NewForm_Click()
    Dim NewForm As Form1
    Set NewForm = New Form1
...
End Sub

Finally, you show the new form:

Private Sub NewForm_Click()
    Dim NewForm As Form1
    Set NewForm = New Form1
    NewForm.Show
End Sub

Calling this subroutine will add as many new forms as you want to a program.

Note that we do not keep track of the new form’s name (NewForm is a local variable in NewForm_Click(), and you can’t use it after returning from that procedure); you might want to save the new forms in an array so you can close them under program control.

Using the code, we create new forms, as shown in Figure 4.15.


Figure 4.15  Creating and displaying new forms.

Using The Multiple Document Interface

You’ve written a new editor program, and it’s a great success. But then you start getting calls from the Field Testing Department: users want to open more than one document at a time. Just how do you do that?

You use MDI forms. MDI frame windows can display multiple child windows inside them; in fact, the Visual Basic IDE itself is an MDI frame window.

For example, if you already have a program based on a single form, Form1, and you want to make that into an MDI child window inside an MDI frame, follow these steps:

1.  Add a new MDI form to the project using the Project[vbar]Add MDI Form item.
2.  Set the MDIChild property of the form you want to use as the MDI child form (Form1 here) to True, as shown in Figure 4.16.


Figure 4.16  Setting a form’s MDIChild property to True.

3.  Run the program; the form you’ve made into the MDI child form appears in the MDI form, as shown in Figure 4.17.


Figure 4.17  Creating an MDI child form


TIP:  In Visual Basic, you can use all kinds of forms as MDI children in an MDI form, as long as their MDIChild property is set to True. You can also use Show() and Hide() on those windows to manage them as you like.

Arranging MDI Child Windows

So you’ve made your program an MDI program, just as the users asked. However, the Testing Department is back on the phone, and they think it would be nice if you could provide some way of arranging the MDI children in the main MDI form so it looks “tidy.”

You could arrange the MDI child forms with their Left, Top, Width, and Height properties, but there’s an easier way—you can use the MDI form method Arrange().

For example, if you add a menu item to an MDI form named, say, “Arrange All,” you can use the Arrange() method to arrange all the windows in the form in a cascade this way:

Private Sub ArrangeAll_Click()
    Me.Arrange vbCascade
End Sub

Using this method results in the cascade of MDI children seen in Figure 4.18.


Figure 4.18  Arranging MDI child forms.

The possible values to pass to Arrange() to specify the way you want to arrange MDI children appear in Table 4.1.

Table 4.1 Ways of arranging MDI child windows.
Constant Value Does This
vbCascade 0 Cascades all nonminimized MDI child windows
vbTileHorizontal 1 Tiles all nonminimized MDI child forms horizontally
vbTileVertical 2 Tiles all nonminimized MDI child forms vertically
vbArrangeIcons 3 Arranges icons for minimized MDI child forms

Opening New MDI Child Windows

Now that you’ve supported MDI, your program’s users want to actually open multiple documents—how can you allow them to do that?

You can do this one of two ways: first, you can create all the forms you want to use at design time and set their Visible properties to False so they don’t appear when the program starts. When you want to show or hide them, you can use Show() or Hide().

You can also create new forms as needed—see “Creating Forms In Code” earlier in this chapter. For example, here we create and display a new MDI child form (assuming Form1’s MDIChild property is set to True), as well as setting its caption:

Private Sub NewWindow_Click ()
    Dim NewForm As Form1
    Set NewForm = New Form1
    NewForm.Caption = "Document"
    NewForm.Show
End Sub

(If you want to display text in these new child forms, you might use a rich text box to cover the form’s client area when you design them.)

We’re adding forms this way in Figure 4.19.


Figure 4.19  Creating new MDI children from code.


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.