By default, Visual Basic loads and displays the first Form of the project. If the loading process takes more’ than a second, the user simply has to wait. The Form- Load application demonstrates a technique for handling slow-loading Forms. You can’t shorten the load time, but you can improve the subjective delay, which is the delay perceived by the user. The LoadForm application takes a while to load two Forms. The delay is artificial, but it simulates a real-world’ condition. The FormLoad application has three Forms, The first one, shown on top of the others, is a simple, quick-loading Form with a couple of messages. You could place your company logo or otheruseiuJ. (or at least less boring) information on the first Form. Initially, the message on the main Form indicates that the application is loading and asks the user to wait. This Form loads the
two slow-loading Forms in the background. As each Form is loaded, the main Form’s caption changes to indicate which Form is currently loading. After both Forms are loaded, the two Command buttons at the bottom of the Form are enabled, and the messages on the Form change to those shown in Figure 4.3. The absolutely essential code for loading the two Forms is as follows:
But if you just use these two lines to load the other Forms of the application, the first Form won’t display properly before the loading process ends. This is because Windows loads the Form in memory and then W:,its for an ol-portunity to draw it Oil the screen. Windows gets this opportunity when other applications aren’t running. The first Form is loaded, then the code on the Form starts executing. While the code is running, nothing happens on the display, To give Windows a chance to load the first Form, display it, and then load the other two Forms, insert the following code in the first Form’s Load event.
If you omit the Forml.Show method, the main Form won’t be displayed before the other two Forms are loaded. The Refresh method tells VIsual Basic to update the display before exec•.t•i.ng the following commands. By default, Visual Basic refreshes the display only when it gets a chance, and this is when it’s not executing any code. If you load a Form from within your code, you must call the Refresh method to redraw the screen instantly.
The program then sets the main Form’s caption to a message indicating that F0pn2 is being loaded and loads Form2. After Form2 is loaded, it does the same for Form3. It displays a similar message in its caption and loads the Form. After both Forms are loaded, it resets its caption and makes the two buttons at the bottom visible so that the usee can display Fonn2 and Form3. The code behind the Show Form2 button is:
and the code behind the Show Form3 button is:
Run the application and see how it works. To contrast this approach with the default behavior OfVlSual Basic, comment out the lines that load the two Forms by inserting a single quote in front of the line you want to comment out; to reinstate the line, remove the comment symbol. If you do so, the Forms will be loaded when the user clicks the corresponding button. They will be loaded just as slowly as before, but there’s a big difference-this delay is perceived by the user. When users click a button, they want something to happen. But with a slow-loading Form, nothing happens for a few seconds, When an application is starting, though, the user will put up with some delay: in other words, the perceived delay. The Forms wili take just as long to load in either case, but loading them when the user isn’t actually working is less obtrusive, .and therefore, mo~e tolerable. A Form that takes more than a second to load at runtime makes your application look amateur-ish, not well designed, and certainly not as user-friendly as it should be. You can simulate the delay by adding the following code ‘to the Load events of Form2 and Form3:
Private Sub Form_LoadC),
LTime’ – TimerO
While TimerC) – LT;me < 5
TIle Timert) function returns the number of seconds elapsed since midnight and is used to delay the loading for five seconds. The total delay is ten seconds, five for each Form. This code works well unless you start the application a few seconds before midnight. The Timert) function is reset at midnight.