Docsity

A guide to Visual Basic Programming, Lecture notes of Computer Science

Partial preview of the text

Download A guide to Visual Basic Programming and more Lecture notes Computer Science in PDF only on Docsity!

VISUAL BASIC

PROGRAMMING

INSTAL VB 13

WEEK 1: BUILDING THE USER INTERFACE

SUBTOPIC

A list of common controls

Difference between Textbox and Label

Calculating using Textbox

Define Lists and Combo box

Adding Items in the list box or combo box

Using the Text Box and Label

  • The textbox is the standard control for receiving input

from the user.

  • The label is not only used to provide instructions and

guides to the users , it can also be used to display

outputs.

  • (^) By the way, a string in a text box can be converted to a

numeric data by using the function Val(text).

Using List box and Combo Box

  • The List Box is to present a list of items

where the user can click and select the items

from the list.

  • The function of the Combo Box is also to

present a list of items where the user can click

and select the items from the list.

WEEK2 VISUAL BASIC VARIABLES

SUBTOPIC

Defining datatypes

Declaring a Variable

Defining arithmetic operators

Calculations using variables

Data type - Specifies type of data variable can store

  • Integer variables: Long, Integer, Short, Byte
  • Floating-point variables: Single, Double
  • Fixed decimal point variable: Decimal
  • Boolean variables: True, False
  • Character variable: Char
  • Text variable: String
  • The Object variable
    • Default data type assigned by Visual Basic
    • Can store many different types of data
    • Less efficient than other data types

Performing Calculations with Variables

 Arithmetic Operators

^ Exponential e.g. 3^2=3*3=

  • Multiplication

/ Floating Point Division e.g. 5/2=2.

\ Integer Division e.g. 5\2=

MOD Modulus (remainder from division) e.g. 7 mod 2=

  • Addition
  • Subtraction

& String Concatenation (putting them together)

e.g. “united” & “states”=united states

Calculating the Area of a circle

  • Private Sub Button1_Click(sender As Object, e As EventArgs)

Handles Button1.Click

  • Const Pi As Single = 3.
  • Dim R As Single = 10
  • Dim AreaCircle As Single
  • (^) AreaCircle = Pi * R ^ 2
  • (^) MsgBox("Area of circle with " & "radius" & R & "=" & AreaCircle)
  • End Sub

Numeric Functions

 Math.Sqrt(X)

 Square root

 Sqrt(4)=

 Math.Int(X)

 Int(2.1234)=

 Math.Round(X)

 To the nearest

integer

 Round(2.45)=

 Math.Abs(X)

 Abs(-34)=

 Math.Log(X)

 Program: calculate the roots

of a quadratic equation given

a, b and c

Private Sub

Quadraticbutton_Click()

Dim A As Integer,=0 B As Integer=

Dim C As Integer=

Dim R1 As Double =0.0, R2 As Double=0.

A = txtA.Text

B = txtB.Text

C = txtC.Text

R1 = (-B+Sqr(B^2-4.0AC))/(2.0*A)

R2 = (-B-Sqr(B^2-4.0AC))/(2.0*A)

outResults.AppendText(“Roots are:”&R1&R2)

End Sub

COM223 13

Example

  • Private Sub Button1_Click(sender As Object, e As

EventArgs) Handles Button1.Click

  • (^) Label1.Text = Math.Round(Val(TextBox1.Text), 2)
  • LblAbs.Text = Math.Abs(Val(TxtNum.Text))
  • LblAbs1.Text = Math.Log(Val(TxtNum.Text))
  • End Sub

The InputBox( ) Function and The

MsgBox( ) Function

  • (^) An InputBox( ) function will display a message box where the user can enter a

value or a message in the form of text.

  • (^) The format is myMessage=InputBox(Prompt, Title, default_text, x-position, y-

position)

  • (^) Dim myitem AS string
  • (^) myitem = InputBox("Enter your Item")
  • (^) MsgBox ( ) Function: Its an output function. Displays a message/output to the user.
  • MsgBox(The sum is & Val(TextBox1.Text) + Val(TextBox2.Text))

WEEK 4: The Selection Process in Visual Basic

 Describe the various types of decisions that can be

made in a computer program.

 Understand the various comparison operators used

in implementing decisions in Visual Basic.

 Use the If-Then-Else, If-Then-Else If, and Case

decision structures.

 A program for grading students marks using if

statement

EXAMPLE

 Dim myNumber As Integer

 myNumber = TextBox1.Text

 If myNumber > 100 Then

 Label2.Text = ” Congratulation! You win a lucky prize”

 Else Label2.Text = ” Sorry, You did not win any prize”

 End If

IF-THEN-ELSEIF DECISION

STRUCTURE

One way to implement a multiple alternative decision

structure is through the If-Then-ElseIf decision structure:

(eliminates need for multiple End If statements)

If condition1 True Then

first set of statements

ElseIf c ondition2 True Then

second set of statements

ElseIf condition3 True Then

third set of statements

Else

last set of statements

End if