Let’s get started using Visual Basic!

Preview:

DESCRIPTION

Let’s get started using Visual Basic!. Private Sub cmdGo_Click...   Dim strMessage As String Dim sngSum As Single If IsNumeric(txtNumber1.Text) = False Then MsgBox "The first box must contain a number." Exit Sub End If - PowerPoint PPT Presentation

Citation preview

Let’s get started using Visual Basic!

Private Sub cmdGo_Click...  Dim strMessage As String Dim sngSum As Single  If IsNumeric(txtNumber1.Text) = False Then MsgBox "The first box must contain a number." Exit Sub End If If IsNumeric(txtNumber2.Text) = False Then MsgBox "The second box must contain a number." Exit Sub End If sngSum = CSng(txtNumber1.Text) + CSng(txtNumber2.Text) strMessage = "The sum of " strMessage = strMessage & txtNumber1.Text & " and " strMessage = strMessage & txtNumber2.Text & " is " strMessage = strMessage & CStr(nSum) MsgBox (strMessage) End Sub

Vocabulary Words

• Compile?– Is when VB pre-translates the entire program

into Native code that the operating system and microprocessor can understand.

– The result is an executable program or EXE file.

– The program runs faster because the translation is already finished.

What are the controls used for?

Open Visual Basic and Create the following program:

txtNumber1

txtNumber2

cmdgo

The Command Button!Event:

Means that when a particular event occurs (when the command button is clicked) the code is executed. We call this event the Click event of the command button.

Open Visual Basic and place two command buttons on the form as shown below:

• Double-click on the form to open the code window.

• From the drop-down list in the upper left of the code window, choose cmdbutton1.

• From the drop-down list in the right corner of the code window, choose the MouseMove event.

• Once the MouseMove event is chosen, it will appear in the code window, followed by an End Sub statement.

• Type the following code:MsgBox(“The cursor is on Button 1”)

• Run your program.• What happens?

The Text Box?

Besides entering text into the a text box, what is the other primary purpose of a text box?

• A text box can also be used by the application to display data for the user to read.

MsgBox?

What is the difference between these two lines of code?

MsgBox(“The cursor is on Button 1”)

MsgBox(TextBox1.Text)

?

What will the following do?

TextBox1.Text = “Hello, World!”

TextBox1.Text = TextBox2.Text

TextBox2.Text = TextBox1.Text

?

The Text Box! Create the following form:

• Create a form that contains two text boxes labeled "First Name" and "Last Name", and a command button labeled "Combine".  

• The text boxes should allow the user to type in anything they want.  

• When the command button is clicked, a message box should appear showing the contents of the text box labeled Last Name followed by a comma and a space and then the contents of the text box labeled First Name.

• When finished, call me and I’ll check your program!

Homework!

• Read section Getting Started. (Its section 1 in your book and section 7 on the computer.)

• Tomorrow we will begin with the listbox and combobox controls!