Sample Code

 

Sample Program 1

 

Note the following:

  • The TabIndex property determines which control gets the focus when the Tab key is pressed. The control with a TabIndex of 0 gets the initial focus. A control receives a TabIndex value based on the order in which it was added to the form. Some controls, such as Labels, are not capable of receiving the focus despite having a TabIndex property.

  • VB creates the event procedure name by appending the event type (Click) to the property Name with an underscore added.
  • Indent statements inside the bodies of event procedures by three spaces to increase program readability.
  • Comments can begin with either ' or REM, and is a single-line comment that terminates at the end of the current line.
    • Comments written to the right of a statement should be preceded by several spaces to enhance program readability.
    • Comments can be placed above the lines of code you are documenting.
    • Precede comments that occupy a single line with a blank line to make the comment stand out and improve program readability.
  • Note the color coding in the programs.  Comments are in green, keywords are in blue, user-supplied code is in black, and undeclared variables have a squiggly underline.

Sample Program 2

The next program obtains Integers from the user, computes the sum and displays the result.

The GUI consists of three Labels, one TextBox and two buttons.

  • The TextBox is the primary control for user input. TextBoxes can also be used to display text.
  • The Text property holds the text that is displayed in the TextBox.
  • The Enabled property determines whether or not users can interact with a TextBox. (If it is False, the user cannot interact with the control. Default True)
  • The MaxLength property value limits how many characters can be entered in a TextBox. (default 32767).
  • To access a property, use the object’s name followed by a period, then the property name:
    inputNumber = txtInput.Text
  • txtInput.Text = " " clears the TextBox.
  • Note the use of comments, as well as the Focus property.
  • An End statement terminates program execution.


Sample Program 3

The final example introduces the InputBox.

The GUI is shown below.

 

  

 

 

The code appears below