|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
Working With Multiple FormsYouve designed your program and its a beauty: an introductory form to welcome the user, a data-entry form to get data from the user, a summary form to display the data analysis results, a logon form to connect to the Internetits all there. Suddenly it occurs to youarent Visual Basic projects organized into modules and forms? How does the code in one form reach the code in anotherthat is, how can the code in the analysis module read what the user has entered in the data-entry form? Its time to take a look at working with multiple forms. For example, lets say that your introductory form looks something like that in Figure 4.12.
When the user clicks the Show Form2 button, the program should display Form2 on the screenand place the text Welcome to Visual Basic in the text box in Form2 as well, as shown in Figure 4.13. To be able to do that, well need to reach one form from another in code.
Create a new Visual Basic project now. This project has one default form, Form1. To add another form, Form2, just select the Add Form item in the Project menu; click on OK in the Add Form dialog box that appears to accept the default new form. In addition, add a new text box, Text1, to the new form, Form2. In addition, add a command button to Form1 and give it the caption Show Form2 and open the code for that button now: Private Sub Command1_Click () End Sub When the user clicks the Show Form2 button, we will show Form2, which we do with Form2s Show() method:
Private Sub Command1_Click()
Form2.Show
...
End Sub
Next, to place the text Welcome to Visual Basic in the text box, Text1, in Form2, we need to use that text boxs fully qualified name: Form2.Text1, indicating that the text box we want is in Form2. We can use that text boxs Text property this way to set the text in the box:
Private Sub Command1_Click()
Form2.Show
Form2.Text1.Text = "Hello from Visual Basic"
End Sub
That completes the code for the Show Form2 button. Form2 has a button labeled Hide Form, and we can implement that by hiding Form2 in that buttons event handler procedure:
Private Sub Command1_Click()
Hide
End Sub
And thats itweve written a program that handles multiple forms.
Loading, Showing, And Hiding FormsThere are times when you might want to work with a form before displaying it on the screen to initialize it (with graphics and so on), in which case you can load the form into memory using the Load statement.
To actually show the form on the screen, then, you use the Show() method. Heres an example in which we load a new form, Form2, and then show it:
Private Sub Command1_Click()
Load Form2
Form2.Show
End Sub
After displaying a form, you can hide it with the Hide() method and unload it (although thats not necessary) with the Unload statement. You usually unload forms if you have a lot of them and are concerned about memory usage. Heres an example in which we hide Form2 and then unload it:
Private Sub Command2_Click()
Form2.Hide
Unload Form2
End Sub
Setting The Startup FormWell, the program is complete, and youve saved writing the best for last: the opening form in which you greet the user. Unfortunately, that greeting form is Form249, and when you actually test the program, Visual Basic pops Form1, which is the Import File dialog box, onto the screen first. How can you make the program start with Form249? You can set the startup form following these steps:
Thats itnow the program will display the form youve selected first when the program runs.
|
|
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. |