|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Creating Forms In CodeYouve added a handy calculator form to your financial planning programbut 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 forms name (NewForm is a local variable in NewForm_Click(), and you cant 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.
Using The Multiple Document InterfaceYouve written a new editor program, and its 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:
Arranging MDI Child WindowsSo youve 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 theres an easier wayyou 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.
The possible values to pass to Arrange() to specify the way you want to arrange MDI children appear in Table 4.1.
Opening New MDI Child WindowsNow that youve supported MDI, your programs users want to actually open multiple documentshow 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 dont 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 neededsee Creating Forms In Code earlier in this chapter. For example, here we create and display a new MDI child form (assuming Form1s 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 forms client area when you design them.) Were adding forms this way in Figure 4.19.
|
|
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. |