134 138




Visual Basic 6 Black Book:Managing Forms In Visual Basic
function GetCookie (name) { var arg = name + "="; var alen = arg.length; var clen = document.cookie.length; var i = 0; while (i < clen) { var j = i + alen; if (document.cookie.substring(i, j) == arg) { var end = document.cookie.indexOf (";", j); if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(j, end)); } i = document.cookie.indexOf(" ", i) + 1; if (i == 0) break; } return null; } var m1=''; var gifstr=GetCookie("UsrType"); if((gifstr!=0 ) && (gifstr!=null)) { m2=gifstr; } document.write(m1+m2+m3);            Keyword Title Author ISBN Publisher Imprint Brief Full  Advanced      Search  Search Tips Please Select ----------- Components Content Mgt Certification Databases Enterprise Mgt Fun/Games Groupware Hardware IBM Redbooks Intranet Dev Middleware Multimedia Networks OS Prod Apps Programming Security UI Web Services Webmaster Y2K ----------- New Titles ----------- Free Archive 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 function isIE4() { return( navigator.appName.indexOf("Microsoft") != -1 && (navigator.appVersion.charAt(0)=='4') ); } function bookMarkit() { var url="http://www.itknowledge.com/PSUser/EWBookMarks.html?url="+window.location+"&isbn=0"; parent.location.href=url; //var win = window.open(url,"myitk"); //if(!isIE4()) // win.focus(); } Search this book:  














Previous
Table of Contents
Next




Working With Multiple Forms
You’ve designed your program and it’s 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 Internet—it’s all there.

Suddenly it occurs to you—aren’t Visual Basic projects organized into modules and forms? How does the code in one form reach the code in another—that is, how can the code in the analysis module read what the user has entered in the data-entry form? It’s time to take a look at working with multiple forms.
For example, let’s say that your introductory form looks something like that in Figure 4.12.

Figure 4.12  A single form that lets the user display another form.
When the user clicks the Show Form2 button, the program should display Form2 on the screen—and 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, we’ll need to reach one form from another in code.

Figure 4.13  A multiform program.
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 Form2’s 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 box’s fully qualified name: Form2.Text1, indicating that the text box we want is in Form2. We can use that text box’s 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



TIP:  One useful property that controls have is the Parent property. Controls are really child windows of the form they’re in, so if you wanted to set the background color of the form that Text1 is in and don’t know that form’s name, you can use the Text1.Parent.BackColor property.

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 button’s event handler procedure:


Private Sub Command1_Click()
Hide
End Sub



WARNING!  If you hide all windows in a Visual Basic program that has no Main() procedure in a module, the program will end.

And that’s it—we’ve written a program that handles multiple forms.


TIP:  You can also make variables global in a Visual Basic project by declaring them at the module level and using the Public keyword. The code in all forms has access to global variables (but in general, you should limit the number of global variables you use so the global space remains uncluttered and you don’t get conflicts and unintended side effects with variables of the same name).

Loading, Showing, And Hiding Forms
There 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.

TIP:  You don’t need to load or unload forms to show or hide them—the loading and unloading processes are automatic. You usually load forms explicitly only to work on them before displaying them, as Visual Basic recommends if you want to work with a form before showing it. However, it actually turns out that you don’t really need to use Load even then, because referring to a form makes Visual Basic load it automatically. This means you don’t have to load forms to use the Show() or Hide() methods with them.

To actually show the form on the screen, then, you use the Show() method. Here’s an example in which we load a new form, Form2, and then show it:


Private Sub Command1_Click()
Load Form2
Form2.Show
End Sub



TIP:  If you load an MDI child window without having loaded its associated MDI frame, the MDI frame is also loaded automatically.

After displaying a form, you can hide it with the Hide() method and unload it (although that’s not necessary) with the Unload statement. You usually unload forms if you have a lot of them and are concerned about memory usage. Here’s an example in which we hide Form2 and then unload it:


Private Sub Command2_Click()
Form2.Hide
Unload Form2
End Sub


Setting The Startup Form
Well, the program is complete, and you’ve 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:

1.  Select the Project[vbar]Properties item.
2.  Select the General tab in the Project Properties box that opens, as shown in Figure 4.14.

Figure 4.14  Setting the startup form.
3.  Set the form you want as the startup form in the Startup Object box, as also shown in Figure 4.14.

That’s it—now the program will display the form you’ve selected first when the program runs.




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.



Wyszukiwarka

Podobne podstrony:
138 142 linuks dla poczatkujacych
138 1S~1
138 menu
Lesson Plan 138 Text

więcej podobnych podstron