The Do Events Statement VB Help

As mentioned earlier, Wmdows doesn’t deal with the display if it’s busy executing code. ‘In other words, the update of the display has a low priority in the Windows “to do” list. ‘The code that implements the artificial delay in the loading of a’ Form is a tight loop. While it’s executing, it takes over the application, You can switch to another application if you wish (Wmdows 95 is a multitasking environment), but the FormLoad application appears to be frozen. For example, you can’t resize the application’s main Form or move it around. Many users like to reposition Forms on the Desktop or resize them when the application starts. The Form- Load application is frozen because the code that’s executing doesn’t give Windows a
.•.charce to handle the display . To.give Windows a chance to do something about the display, you must call the DoEvents statement. The DoEvents statement tells your application to give Windows a ~ce to take care of its own chores. It doesn’t relinquish control to the
operaung syst emi it simply gives it a little tim» to take care of any pending tasks,

CMld thr: control is returned immediately to the application. To experiment with the DoEvents statement, modify the While Idbp as shown here:

Private Sub Form_Load()
Dim LTime
LTime ~ TimerO
While Timer() LTime < S
DoEvents
Wend
End Sub

Now run the application and see how it reacts when you attempt to reposition or resize the main Form while the two Forms are being loaded, .The application i no longer frozen. It reacts to external events as it should.

The DoEvents statement canintroduce significant delays. If you. a loop that might execute a million times, you can’t return control to the operating system with’ each iteration. Each time a new process takes control of the CPU, Some overhead il ‘required. It doesn’t take long, but if you add up these delays, the DoEvents statement may harm your application rather than help it. Using the DoEvents statemer ,effectively takes some experience and.experimentation. We will come back to this topic, Drawing with VISual Basic, where we’ll look at techniques for balancing the requirements of quick code execUtion and proper application behavior.

Controlling One Form from within Another

You’ve learned how to load a Form from within another. In many situations, this is all the interaction you need between Forms. Each Form is designed to ‘operate independently of the others, but they can communicate via global variables (see the discussion “Global versus Public Variables,” later in the chapter). in other situations, however, you may need to control one Form from within another. Controlling the Form means accessing its controls and setting or reading its values from within another Form’s code.
Look at the two Formsin, for instance. These are Forms of the TextPad application, which we are going to develop in Chapter 5, Basic ActiveX Controls. TextPad is a text editor which consists of the main Form and an auxiliary Form for the Search & Replace operation. All other operations on the text are performed with the commands of the menu you see on the main Form, When the user wants to search for and/or replace a string, the program displays another Form on which the user specifies the text to find, the type of search, and so on. Wher:t the user clicks onone of the Search & Replace Form’s buttons, the corresponding code must access the text on the main Form of the’ application and search for a word 01′ replace a string with another. You’ll see how this works in Chapter 5. hi this chapter, we’ll develop a simple example to demonstrate how you can access another Form’s controls.
Accessing a Form’s controls from within another is straightforward. Simply prefix the control’s name with the name of the Form. For example, to access the Text propertyof the Text! control on the Form, you could use the following expression:

Textl. Text

To access the same property from within another Form, you would use this expression:

Fom 1.Textl. Text

Form 1 is the name of the Form to which the Text~ control belongs. You can actually have a Text! control on two Forms. The expression Textl. Text is the Text property of the TeXt! control on either control. If the following statement is.executed from within Forml, it assigns the string “some text” to the Textl control on Forml:

Capture

If the same statement is executed from within Form2, it assigns the same string to the Textl control on Form 2. To access the control on the other Form, you would prefix the’ expression with the name of the Form.

You can actually manipulate the properties of the controls on a Form that is not loaded. Vijual Basic loads the Form and changes its property, but doesn’t display the Form: When the Form is later displayed, you’ll see that the property has Changed·visual

Global versus Public Variables

The simplest method for two Forms to communicate with each other is via global variables. These variables are declared in the application’s Module with the Global or Public keyword. If the following declarations appear in a Module, then they can be accessed from any place in the application’,s (any Form or other Module) code:
Global NumPoints As Integer
Public DataValues(lOO) As Double

A Form’s code, however, can’t access variables declared in another Form directly. Two or more Forms can have a variable called iCounter, but each iCounter variable is separate and exists only in the contextof the Form in which it has been declared. It’s also possible to access a variable declared in one Form from within another, as long as one of these Forms “exposes” the variable by declaring it as Public. To access public variables on one Form from within another Form’s code, you must use an expression like the following one: CustName – Fonm2.FName & “,”& Form2.LName The variables FName and LName must be declared in Form2 with the following statement:

Public FName As String, LName As String

The PVars project on the CD demonstrates how to access the public variables of a Form from within another Form’s code. The application consists of two Forms, Fornil and Formz,

Capture

The code in Form2 consists of the declarations of two variables and the Commandbutton’s Click event that sets these two variables. Here’s the entire code listing of Form2:

Public FName As String, LName As String
Private Sub Commandl_Click()
LName – “Wong”
FName – “Kevin”
End Sub

The variables FName and LName are declared as Public, so that they can beaccessed from outside the Form’s code. Toassign some value to the  variables, click the Command button on Forin2. ‘Then, you can retrieve their values from within Forml. The code behind the Read Customer Name button on Form 1  is:

The Form2.Showstatement in the Form’s Load event displays the second Form,where you can set the values of the public variables

 VB6 at Work: The Fo’rms Project

The Forms application demonstrates the Load and Unload statements and the Show and Hide methods as well as how to accessthe properties and methods of one Form from within another, The application’s main Form, shown in Figure 4.6, is called Form Handling. This Form contains two groups of identical controls under jhe headings Fonn2 and Form3. (Only Fonn2 is shown in Figure 4.6. Form3 is identical to Fonn2; it just contains a different graphic.) These two Forms are ioaded, shown, and hidden from within the main Form. You can also specify whether the Forms will be shown as modal or modeless.

Capture

After Form2 and Form3 are loaded, you can use the buttons in the lower half of the main Form to set their BackColor and Caption properties and to call their Hide method.Let’s look at the code behind the various buttons of the Forms application’s main Form.

The Load Form2 button is a toggle. If the Form is already loaded, it unloads it and sets its own caption accordingly. The Show Form2 button calls the Show method to show Form2 (whether or not it’s been loaded). The code behind the ;ihow Form2 button takes into consideration the state of the Show Modal checkbox. If.it’ s checked, it shows the Form as modal.

The Show Form2 Button

Private Sub ShowForm2_CllckCJ
Fonm2.Show Form2Modal.Value
End Sub

The checkbox’s Value property is 0 or I, which} ‘lappel’lSto be the same as the valid values of the Show method’s Mode argume-nt ‘That’s why we can pass the checkbox’s Value property as argument to the Sl lOW method. The Background Color button sets the background color of Fcrmz to a random color.

The Background Color But can

Private Sub Fonm2Color_Click()
Form2.BackColor – QSColorCRnd() * 15)
End Sub

The Form’s Caption button sets the caption of the Form with the following statement

The caption Button

Private Sub Form2Capti on_Cli ckO
Form2.Caption = ‘Caption changed!’
End Sub

Finally, the Hide Form button hides the Form by calling its Hide method.

The Hide Form Button

Private Sub HideForm2_Click()
Form2.Hide
End Sub

The code behind the buttons of the second group on the Forms application is quite similar, only it refers to Form3 instead of Form2.

Posted on November 4, 2015 in Working with Forms

Share the Story

Back to Top