new project in visual basic please use speaker notes for additional information!

15
New Project in Visual Basic Please use speaker notes for additional information!

Upload: dina-holmes

Post on 03-Jan-2016

216 views

Category:

Documents


1 download

TRANSCRIPT

New Project in Visual Basic

Please use speaker notes for additional information!

New ProjectNew Project

This project is called Project1 by default and the form is called Form1 by default.

The properties are showing the default properties for the form.

New ProjectNew ProjectI clicked on Project1 in the Project Window and the project was brought up in the Properties window. I then entered the new name under Name. The project is now called New Project. Notice that it also appears on the window above the form.

New FormNew FormI clicked on Form 1 under Forms which is under NewProject and this brought up the properties for the form. I then named the form NewForm.

Putting on a label

Putting on a label

The first thing I want to put on the form is a label box. I drag and drop and reposition it to the place where I want it. The properties are then used to name the label box and make other property changes.

Note that the … when I click on font means that more information is available if you click on the …

This brought up the font window.

Another labelAnother label

Text BoxText Box

This is a textbox that I named txtFirst.

txtFirst was keyed into the Name property which does not show because I have scrolled past it.

ButtonButtonI have now established a command button which I named cmdCalc. I will now need to write the code to make clicking on cmdCalc cause the addition of the numbers in txtFirst and txtSecond and the display of the answer in txtTotal.

Button - click event

Button - click event The code shown below is the code that will be

executed when the Calculate button is clicked.

Note that you can see that this is a subroutine that is executed when cmdCalc is clicked and you can also see the button and the event in separate windows.

Run/ExecuteRun/ExecuteTo Run or Execute, click on Run. The form will come up in Run/Execute mode. I then entered the two numbers and clicked the Calculate button which calculated and showed the results.

ProArithProArith

VB for ProArithVB for ProArith

Option Explicit

Private Sub cmdAdd_Click()txtTotal = Val(txtNum1) + Val(txtNum2)End Sub

Private Sub cmdClear_Click()txtNum1 = " "txtNum2 = " "txtTotal = " "txtNum1.SetFocusEnd Sub

Private Sub cmdDiv_Click()txtTotal = Val(txtNum1) / Val(txtNum2)End Sub

Private Sub cmdExit_Click()EndEnd Sub

Private Sub cmdMult_Click()txtTotal = Val(txtNum1) * Val(txtNum2)End Sub

Private Sub cmdSubt_Click()txtTotal = Val(txtNum1) - Val(txtNum2)End Sub

VBVBAdds the contents of txtNum1 which is converted to a number and txtNum2 which is also converted and assigns the results to the txtTotal box.

Sets all of the boxes back to spaces and then sets the focus (puts the cursor) in txtNum1.

The End statement will stop the processing of the form.

txtNum1 is divided by txtNum2 and the results are assigned to txtTotal.

These two events are to multiply and subtract.

Note that the multiply uses the *.

Run ProArithRun ProArithThe SUBTRACT button was clicked.

ProArith1ProArith1 Option Explicit

Private Sub cmdAdd_Click()txtTotal.Text = Val(txtNum1.Text) + Val(txtNum2.Text)End Sub

Private Sub cmdClear_Click()txtNum1.Text = " "txtNum2.Text = " "txtTotal.Text = " "txtNum1.SetFocusEnd Sub

Private Sub cmdDiv_Click()txtTotal.Text = Val(txtNum1.Text) / Val(txtNum2.Text)End Sub

Private Sub cmdExit_Click()EndEnd Sub

Private Sub cmdMult_Click()txtTotal.Text = Val(txtNum1.Text) * Val(txtNum2.Text)End Sub

Private Sub cmdSubt_Click()txtTotal.Text = Val(txtNum1.Text) - Val(txtNum2.Text)End Sub

.Text after the textbox name says that we are specifically interested in the Text property of the box.