CSC110 Introduction to Computer Science

Total Page:16

File Type:pdf, Size:1020Kb

CSC110 Introduction to Computer Science

1

CSC110 Introduction to Computer Science Subject: Visual Basic Programming

Our goals for this part of the course are to learn how to think logically and how to write programs to control the behavior of a computer.

Visual Basic is one of the high level programming languages available under Microsoft Windows. Visual Basic 2008 Express Edition is an Integrated Development Environment (IDE) that can be downloaded free of charge from the following web address http://www.microsoft.com/express/download/. In our lab, this “environment” has been set up for us. We need to click on Start > All Program > WCU > Visual Basic 2008 Express Edition to start the expedition. If there is any error, you need to report it to the ACC Help Desk office.

1. Getting started with the IDE – Watch the Video that came with the IDE and perform the task described in the video to develop a web browser. 2. Understanding several user controls – ListBox, Button, RadioButton, TextBox

In this lab exercise, we will create a form with a Label, a TextBox, three RadioButtons, and a Button as follows:

Option Strict Off 2

Option Explicit On Friend Class frmType Inherits System.Windows.Forms.Form

Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click End End Sub

Private Sub optLuxury_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optLuxury.CheckedChanged

txtAmount.Text = 5000

End Sub

Private Sub optWithMeal_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optWithMeal.CheckedChanged

txtAmount.Text = 3500

End Sub

Private Sub optWithoutMeal_CheckedChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles optWithoutMeal.CheckedChanged

txtAmount.Text = 2500

End Sub

End Class 3

Assignment 1: Design a web form application with a Label, a TextBox, and a Button. When a user enters a text string and clicks the button, the label is replaced with the string the user entered.

In our first VB project example, we’ve learned that the process to complete a “windows forms project”. To complete this assignment, we need to follow the procedure below: 1. Design a form with some controls. We recommend that the names of the controls be changed to follow the naming convention: TextBox – txt Button – cmd Label – lbl ListBox – lst The reason is for the ease of remembering the names of these controls during the next step when we add some code to our project. 2. Add some code to control the behavior when some events occur at the time the project is executed, e.g., a user clicks a button, a user changes the content of a ListBox control, etc. The way to add some code is to be able to jump between the Design View to the Code View window and back. You can click F7 to jump from the Design View to the Code View. You can also “double-click” the command button to jump to the Code View if the event is “clicking a button”. (THINK ABOUT THIS FOR A WHILE NOW) You need to add the code to the event handling routing associated with the control that caused the event, i.e., the button. 3. After you double-click the button, you can add some code to complete the “event handling routing”. What you need to do depends on the requirement. If the textbox control will be changed after the user clicks the button, the code is: 4. Now we can test the code by click F5 or Debug tab>Start Debugging. 4

3. More user controls: MsgBox, If statements, ListBox

In this project, a user can enter a name and click the Add button to add the name to the ListBox. Also, the amount entered will be displayed in a textbox window. The source code must be entered to handle the events such as when “Add” is clicked. Do you know how to add the following code into the form.vb file?

When you complete your design of the form, you need to change the names of these controls to reflect the “types” of the controls for the reason to remember the names of them. For example, we need to use a prefix of “txt” to be the prefix for a TextBox; a prefix of “lbl” in front of a label; a prefix of “lst” in front of a “ListBox”; a prefix of “cmd” in front of a command Button.

In this project, we also learn how to choose the item inside of a ListBox using the SelectedIndex property of a ListBox control. Please refer to the code for the cmdRemove_Click() sub-procedure for details.

For displaying the amount entered, you may use a Label control with the name lblDisplay.

Option Strict Off Option Explicit On Friend Class frmListBox Inherits System.Windows.Forms.Form Private Sub cmdAdd_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAdd.Click lstName.Items.Add(txtName.Text)

txtName.Text = "" 'Clearing the text box txtName.Focus() ‘Move the cursor to the text box lblDisplay.Text = lstName.Items.Count cmdAdd.Enabled = False ' Disabling the Add button

End Sub 5

Private Sub cmdClear_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdClear.Click lstName.Items.Clear() lblDisplay.Text = lstName.Items.Count End Sub

Private Sub cmdExit_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdExit.Click Me.Close() End Sub

Private Sub cmdRemove_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdRemove.Click

Dim remove As Short remove = lstName.SelectedIndex 'Getting the index If remove >= 0 Then lstName.Items.RemoveAt(remove) lblDisplay.Text = lstName.Items.Count MsgBox("Index " + lblDisplay.Text + " Removed") End If

End Sub

Private Sub txtName_TextChanged(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles txtName.TextChanged

If (Len(txtName.Text) > 0) Then cmdAdd.Enabled = True End If End Sub End Class

Assignment 2: Change the above exercise and allow a user to enter only names with less than 5 letters. If a user enters any invalid names, a message box will be used to display the error message. 6

4. Computations

A simple calculator project is used to illustrate the operations a computer can perform. In this project, each key represents a command button. In the code, two variables are defined, i.e., previous and current. The previous variable is used to store the value of the first operand and the current variable is used to store the second operand. When any key is pressed, the sub-procedure cmd_Click() is executed to store the digit user entered into the current variable after the current is stored into the previous variable. So when the equal key is pressed, there are two operators. Based on the operator the user chooses, the sub-procedure cmdEqual_Click() is executed to compute the result of the operation.

When you complete the design, you need to name each key as follows:

AC = cmdAC 0 – 9 = cmd_0 - cmd_9 dot = _cmd_10 + = cmdPlus - = cmdMinus * = cmdMultiply / = cmdDivide +/- = cmdNeg TextBox1 = txtDisplay 7

Public class Form1

Dim Current As Double Dim Previous As Double Dim Choice As String Dim Result As Double

Private Sub cmd_0_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmd_0.Click

txtDisplay.Text = txtDisplay.Text & "0" Current = Val(txtDisplay.Text) End Sub

‘ This line is a comment ‘ You need to add cmd_1_Click to cmd_9 similarly to cmd_0_Click ‘ here.

Private Sub cmdAC_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdAC.Click Current = Previous = 0 txtDisplay.Text = "" End Sub

Private Sub cmdDivide_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdDivide.Click txtDisplay.Text = "" Previous = Current Current = 0 Choice = "/" End Sub

Private Sub cmdMinus_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdMinus.Click txtDisplay.Text = "" Previous = Current Current = 0 Choice = "-" End Sub

Private Sub cmdMultiply_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdMultiply.Click txtDisplay.Text = "" Previous = Current Current = 0 Choice = "*" End Sub

Private Sub cmdPlus_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdPlus.Click txtDisplay.Text = "" Previous = Current Current = 0 8

Choice = "+" End Sub

Private Sub cmdEqual_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdEqual.Click

Select Case Choice

Case "+" Result = Previous + Current txtDisplay.Text = Result Case "-" Result = Previous - Current txtDisplay.Text = Result Case "*" Result = Previous * Current txtDisplay.Text = Result Case "/" Result = Previous / Current txtDisplay.Text = Result End Select

Current = Result

End Sub

Private Sub cmdNeg_Click(ByVal eventSender As System.Object, ByVal eventArgs As System.EventArgs) Handles cmdNeg.Click Current = -Current txtDisplay.Text = Current End Sub

End Class

Assignment 3: Modify the previous lab project to add a “Power” operation. You need to enter two numbers a and b. The Power operation finds the result of “a to the power of b” and displays the result. You can use the symbol “^” for the “Power” operation.

Hint:

1. You need to change the design and add a command Button for the Power operation. You may use the sign “^” for the operation. 2. You need to add similar statements to those in cmdPlus with the Choice changed to “^” 3. In the cmdEqual, you need to add another case.

Recommended publications