The States of Visual Basic VB Help

Let’s explore the role of the Immediate window while the application is in break state. Place the pointer in the Immediate window and type the following:

Print Examplel.Caption

As you .recall, Examplel is the name of t e Form, and Caption is the name of the property that sets or reads the Form’s caption. The previous statement displays the form’s caption in the line below as soon as you press Enter.

The following statement displays the value 3:

Print (1+2+3+4+5)/ 5

This is a regular Visual Basic statement consisting of numeric values and arithmetic operators. It can appear anywhere in a Visual Basic application. You can issue all types of Visual Basic statements in the Immediate window, such ;IS cos(3/100) or Rndt), which returns a random number between 0 and 1.

The last statement shown:

Examplel.Caption – ‘Remote Control’

does something quite interesting. It sets the value of the Form’s Caption property. To display the application’s window, press Alt+ Tab. You will see that its caption has changed, as shown.

You use the Immediate window when you are designing and debugging applications to perform three common operations:

• Execute Visual Basic statements to perform simple tasks such as calculations
• Examine the values of the controls on the Form
• Set the values of the controls on the Form.

These are extremely useful operations during an application’s design and debugging phase. You don’t have to write code to perform simple tasks. Merely type a statement in the Immediate window, and it executes as if it belongs to the current application.

You can issue even more complicated statements in the Immediate window, as long as you type them all on the same line. To display six random numbers in the range 1 to 49, for instance, you can either issue six separate Print statements or write a small For … Next loop that displays the numbers. In an application’s code, you would use the following structure:

For lucky = 1 To 6
Print 1+ Int(Rnd()*49)
Next lucky

To issue the same statements in the Immediate window, you must type them in a single line ..To enter more than one statement in a line (whether in the Immediate window or in your code), use the colon a~ separator:

For lucky = 1 To 6: Print 1 + Int(Rnd()*49): Next lucky

Enter this line in the Immediate window and press Enter to display the next six lucky numbers, as shown:


Posted on November 4, 2015 in Getting Started with Visual Basic

Share the Story

Back to Top