0% found this document useful (0 votes)
6 views31 pages

PDF

The document outlines a course in Programming Visual Basic 6.0, detailing its importance, variable types, operators, and the structure of a Visual Basic application. It includes various projects and solutions demonstrating mathematical functions, user interface design, and programming logic. The course is supervised by Assistant Professor Dr. Ahlam M. Kadhim from Mustansiriyah University.

Uploaded by

yzulkiflu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views31 pages

PDF

The document outlines a course in Programming Visual Basic 6.0, detailing its importance, variable types, operators, and the structure of a Visual Basic application. It includes various projects and solutions demonstrating mathematical functions, user interface design, and programming logic. The course is supervised by Assistant Professor Dr. Ahlam M. Kadhim from Mustansiriyah University.

Uploaded by

yzulkiflu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 31

See discussions, stats, and author profiles for this publication at: https://www.researchgate.

net/publication/364327123

Course in Programming Visual Basic 6.0 Second year

Chapter · October 2022

CITATIONS READS

0 3,804

1 author:

Ahlam M. Kadhim
Mustansiriyah University
25 PUBLICATIONS 37 CITATIONS

SEE PROFILE

All content following this page was uploaded by Ahlam M. Kadhim on 14 October 2022.

The user has requested enhancement of the downloaded file.


Mustansiriyah University ‫الجامعة المستنصرية‬

College of Science ‫كلية العلوم‬

‫قسم الفيزياء‬
Department of Physics

Course in
Programming
Visual Basic 6.0
Supervised and prepared by
Assistant Professor Dr. Ahlam M. Kadhim

Second year
What is Visual Basic?
Visual Basic is one of the most important and powerful programming
languages that are widely used nowadays. Visual Basic is a tool that
allows you to develop Windows (Graphic User Interface - GUI)
applications. Visual Basic is governed by an event processor. Nothing
happens until an event is detected. Once an event is detected, the code
corresponding to that event (event procedure) is executed. Program
control is then returned to the event processor. The use of this language is
due to two main reasons:
1. The ease of learning this language and the breadth of its
applications.
2. Continuous support from Microsoft in updating this language and
adding many features that address many modern topics.

Variables Types
Type Description
String Symbolic Variable
Boolean Logical variable True or False (0, 1)
Integer -32768 to 32767
Long -2x109 to 2x109
Single 1.40129x10-45 to 3.40283x1038
Double 4.94065x10-324 to 1.7976x10308
Date January 1st 100 To December 31st 9999
Variant All variables

Numerical Operators

Operator Name Function


+-*/ Basic math operators
^ Exponent Takes a number to the given exponent
Int Integer Strips off any digits after the decimal. It
does not perform rounding
Abs Absolute value Removes any sign from a number
Sgn Sign Extracts the sign from a number. 1 for
positive -1 for negative and 0 for zero.
Exp Antilog Returns a number specifying e raised to
a power
Sin Sine Std trig calculation
Uses RADIANS
Cos Cosine Std trig calculation
Uses RADIANS
1
Tan Tangent Std trig calculation
Atn Arctangent Std trig calculation
Uses RADIANS
Log Logarithm Natural logarithm ( e based 2.718282 )
Rnd Random Generates a random positive number
between 0 and 1
Sqr Square root Takes the square root of a number
Atn Arctangent Std trig calculation
Uses RADIANS
Log Logarithm Natural logarithm ( e based 2.718282 )
Rnd Random Generates a random positive number
between 0 and 1
Sqr Square root Takes the square root of a number
Logical Operators

Command Name Description

NOT Invert In case of true false it inverts the condition. In case


of a number every bit is flipped.
OR Or Standard logic operator Output is true when one
input term is true

AND And Standard logic operator Output is true when all


input terms are true
XOR Xor Standard logic operator Output is true if one and
only one of the 2 inputs is true

Structure of a Visual Basic Application Project

Application (Project) is made up of:


1. Forms - Windows that you create for user interface
2. Controls - Graphical features drawn on forms to allow user
interaction (text boxes, labels, scroll bars, command buttons, etc.)
(Forms and Controls are objects.)
3. Properties - Every characteristic of a form or control is specified
by a property. Example properties include names, captions, size,
color, position, and contents. Visual Basic applies default
properties. You can change properties at design time or run time.
2
4. Methods - Built-in procedure that can be invoked to impart some
action to a particular object.
5. Event Procedures - Code related to some object. This is the code
that is executed when a certain event occurs.
6. General Procedures - Code not related to objects. This code must
be invoked by the application.
7. Modules -Collection of general procedures, variable declarations,
and constant definitions used by application. Steps in Developing
Application·
There are three primary steps involved in building a Visual Basic
application:
1. Draw the user interface
2. Assign properties to controls
3. Attach code to controls

Project1: Write a program to print the results of each


mathematical equation from the following. Choose the values of
the variables for each equation?
4πa3
1. y=b+
3
2. y = 2a + b − 1
3. y = (2a − 3b)/(a + b)
4. y = √a2 +b 2
5. y = b2 − (4ac)

Solution:

Dim a, b, y As Double
Private Sub Command1_Click ()
a = Val (Text1.Text)
b = Val (Text2.Text)
Pi = 3.14
y = b + (4 * pi * a ^ 3) / 3
Text3.Text = y
End Sub

Homework: Write other programs for the rest of the equations


using other tools?

3
Visual Basic also includes two additional arithmetic operators,
the division sign / gives the result of an integer and the division
sign \ (backslash) that gives the result of integers and fractions

Project 2: Solve the following equations and disiplay the


output of division with fractions and without fractions numbers
and the remainder of division (choose the appropriate tools). as
in the following example:

Solution:

Private Sub Command1_Click()


X = 13 Mod 5
Print X
End Sub
Mathematical Function

Function Task
X=RND Create random number value (0…1)
Y=ABS(X) |𝑥|
Y=SQR(X) √𝑥
Y=EXP(X) ex
Y=LOG(X) Len(x)
Y=LOG(X) / LOG(10) Log(x)
Y=sin(X)
Y=cos(X)
Trigonometric function
Y=tan(X)

4
Y=ATN(x) Tan-1(x)
Y=INT(X) Take rounding value of x
Y=FIX(X) Take only integer part
Conversion Functions:

Visual Basic provides several functions to convert between


different data types as shown in the table:
Function Task
CDate Date ‫تحويل المتغير الى النوع‬
CInt Integer ‫تحويل المتغير الى النوع‬
CLng Long ‫تحويل المتغير الى النوع‬
CSng Single ‫تحويل المتغير الى النوع‬
CDbl Double ‫تحويل المتغير الى النوع‬
CVar Variant ‫تحويل المتغير الى النوع‬
CStr String ‫تحويل المتغير الى النوع‬
Val String ‫تحويل المتغير الى النوع‬

Project 3: Design an interface to test all variable functions in


the above table for converting any numeric variable from the
following to other variable types?

X1= 2.12345, X2= - 6.9876, X3= 3.789, X4=-4.2345

Solution:
Dim X As Double
Private Sub Command1_Click( )
X = InputBox("Enter Number")
List1.AddItem y
End Sub
Private Sub Command2_Click( )
y = CLng(X)
List1.AddItem y
5
End Sub

Private Sub Command3_Click( )


y = Csng(X)
List1.AddItem y
End Sub
Private Sub Command4_Click( )
y = Cstr(X)
List1.AddItem y
End Sub

Project 4: Write a program to create a structured calculator that


performs arithmetic operations for any number you enter into
the program ABS SQR, EXP, LOG, sin, cos, tan INT and FIX?
Solution:

Private Sub Command1_ click ( )


a= Val (Text1.text)
X =log(a)
Text2.Text = CStr (X )
End Sub

H.W: Type other codes to find all the


functions and choose the appropriate tools?

Project 5: Write a program code that shows the use of the Rnd
function to randomly choose among ten numbers, when a certain
number appears, the image is displayed and the program plays
the sound?
Solution:

Private Sub Command1_Click)(


Image1.Visible = False ' hide coins
Label1.Caption = Int(Rnd * 10)
Label2.Caption = Int(Rnd * 10(
Label3.Caption = Int(Rnd * 10(
' if any caption is 7 display coin stack and beep
6
If (Label1.Caption = 7) Or (Label2.Caption = 7) Or (Label3.Caption = 7)
Then
Image1.Visible = True
Beep
End If
End Sub
Private Sub Command2_Click)(
End
End Sub

Project 6: Write a Visual Basic program, an interface that


contains three buttons "red", "green" and "blue" when you press
one of these buttons, the color of the new form changes, choose
the required tools?

Solution: HScroll1: Max 255


HScroll2: Max 255
Private Sub Command1_Click)( HScroll3: Max 255
Form1.BackColor = Command1_:caption Chan
RGB(HScroll1.Value, ge
HScroll2.Value, HScroll3.Value( form
color
End Sub

Private Sub HScroll1_Change)(


Label1.Caption = HScroll1.Value
End Sub

Private Sub HScroll2_Change)(


Label2.Caption = HScroll2.Value
End Sub
Private Sub HScroll3_Change)(
Label3.Caption = HScroll3.Value
End Sub

7
Project 7: Write the previous program using other tools?

Solution:

Private Sub Option1_Click()


Form1.BackColor = vbGreen
End Sub

Private Sub Option2_Click()


Form1.BackColor = vbBlue
End Sub

Private Sub Option3_Click( )


Form1.BackColor = vbRed
End Sub

Project 8: Write a program to write your personal information,


show the results using the Listbox tool?

Solution:

Private Sub Command1_Click()


X1 = InputBox)"Enter Your Name","Name")
List1.AddItem "Name: " & X1
X2 = InputBox)"Enter Your Age","Age")
List1.AddItem " Age: " & X2
X3 = InputBox)"Enter Address"," Address")
List1.AddItem " Address: " & X3
End Sub

Project: Write a program to calculate the area of the following


geometric shapes, and then display the results in a text box,
Choose the appropriate tools? Circle 𝐚𝐫𝐞𝐚 = 𝝅 ∗ 𝑹𝟐

Private Sub Command1_Click ()


Dim R ,Area As Double
8
R = InputBox)"Enter Circle Reduce")
PI = 3.14159
Area = PI * R^2
Text1.text= Area
End Sub

HW: Write other programs to calculate the area of each one of


the geometric shapes?
Rectangle 𝐚𝐫𝐞𝐚 = 𝐀 ∗ 𝐁
𝟏
Triangle 𝒂𝒓𝒆𝒂 = 𝑯 ∗ 𝑩
𝟐
Project 9: Write a program to calculate the average, total
summation, and final result for student exam in three subjects.
Use appropriate tools to enter grades and others tools to display
the final results?

Private Sub Command1_Click()


Text1.Text = ""
Text2.Text = ""
Text3.Text = ""
Text4.Text = ""
Text5.Text = ""
Text6.Text = ""
End Sub
Private Sub Command2_Click()
End
End Sub
Private Sub Command3_Click()
Text4.Text = Val(Text1.Text) + Val(Text2.Text) +
Val(Text3.Text)
Text5.Text = Val(Text4.Text) / 3
x = Val(Text5.Text)
If x < 50 Then
Text6.Text = "Fail"
ElseIf x <= 60 Then
Text6.Text = "Medium"
9
ElseIf x <= 70 Then
Text6.Text = "Good"
ElseIf x <= 80 Then
Text6.Text = "Very Good"
ElseIf x <= 90 Then
Text6.Text = "Exlent"
ElseIf x < 100 Then
Text6.Text = " Very Exlent "
End If
End Sub
Project 10: Write a code program to design a simple calculator.
This program uses a combo box which includes four operators,
addition, subtraction, multiplication and division and two list
boxes included the integer numbers. It can perform the above
four basic calculations by changing the operators.

Solution:
Private Sub Form_Load()
Dim I
For I=1 To 10
List1.AddItem Str(I)
List2.AddItem Str(I)
Next I
Combo1.AddItem “+”
Combo1.AddItem “-”
Combo1.AddItem “*”
Combo1.AddItem “/”

End Sub

Private Sub Command1_Click()


Dim z
Select Case Combo1.Text
Case "+"
z = Val(List1.Text) + Val(List2.Text)
10
Case "-"
z = Val(List1.Text) - Val(List2.Text)
Case "*"
z = Val(List1.Text) * Val(List2.Text)
Case "/"
z = Val(List1.Text) / Val(List2.Text)
Case Else
Exit Sub
End Select
Text1.Text = Str(z)
End Sub

Project 11: Write a program to print a list of numbers for an


array in (1-D) and displays the sum of odd sequence numbers
and the sum of even sequence numbers, use another Visual
Basic tools ?

Solution:

Private Sub Comente_Click


A=0
B=0
Dim x(5)
For i = 1 To 5
x(i) = InputBox("inter the number")
If I /2 = Int( i / 2 ) Then
A = A + x(I )
Print x(I )
Else
B = B + x(I )
Print x(I (
End If
Next
Print " SUM EVEN= " & A
Print " SUM ODD= " & B
End Sub

11
Project 12: Write a program to enter any list of numbers such as
(3,7, 9, 6, 10,4,20 and 40), print the numbers that are divisible
by a certain number (5, 2) display them by one V.B tools ?

Solution:

Private Sub Command1_Click()


A = Val(InputBox("Enter Division number"))
List1.AddItem " Division number is : " & A
For i = 1 To 5
x = Val(InputBox("Enter List Number"))
If x / A = Int(x / A) Then
List1.AddItem x
End If
Nex
End Sub

Project 13: Write a program to print the


grade table for a group of students for
specific study subjects?

Solution:

Private Sub Command1_Click)( Math Physics Sports


Ali 90 80 70
Ahmed 70 90 80
Dim name As String, degree As Integer
Print Space(10); "Math " & " Physics " & " Sports"
For i = 1 To 2
name = InputBox("Enter Student Name)"
Print name & Space(6;)
For j = 1 To 3
degree = Val(InputBox("Enter Degrees))"
Print degree & Space(6;)
Next
Print
Next
End Sub
12
Project 14: Design Project to find the value of the following
series. 𝐒𝐮𝐦 = ∑𝐢=𝐍
𝐢=𝟏 𝐚 ∗ 𝐢 + 𝐛 , The constants value a, b are
entered into text boxes. When the users click checkbox to find
Sum when N= 20. If checkbox is unchecked the terms N is
entered into input box. Display the Sum in a separate text box?

Solution:

Private Sub Command1_Click ( )


Dim a, b, Sum, Na = Val (Text1.Text)
b = Val (Text2.Text)
Sum = b
If Check1.Value = 1 Then
For I = 1 To 20
Sum = Sum + a * I
Next
Else
N = Val (inputbox (“No. of terms=”))
For I = 1 To N
Sum = Sum + a * I
Next
End If
Text3.Text = Str (Sum)
End Sub

Project 15: Write a program like word program consisting of


three groups (cxombo box) to change the font size, font type and
font color for text written inside a text box?
Solution:
Combo1: Event Click
Private Sub Form_Load)( Combo2: Event Click
For s = 1 To 25 Combo3: Event Click
Combo1.AddItem s Command1_:caption Clear Text
Next Command2_:caption End
For s = 0 To Screen.FontCount Label1_:caption Font Size
Combo2.AddItem Label2_:caption Font Style
Screen.Fonts(s) Label3_:caption Font Color
13
Next
For s = 1 To 15
Combo3.AddItem s
Next
End Sub
Private Sub Combo1_Click)(
Text1.FontSize =
Combo1.List(Combo1.ListIndex)
End Sub

Private Sub Combo2_Click)(


Text1.Font =
Combo2.List(Combo2.ListIndex)
End Sub

Private Sub Combo3_Click)(


Text1.ForeColor = QBColor(Combo3.ListIndex)
End Sub

Private Sub Command1_Click)(


Text1.Text"" =
End Sub

Private Sub Command2_Click)(


End
End Sub

14
1–D Arrays
Arrays are declared in a manner identical to that used for regular
variables. For example, to declare an integer array named ‘A',
with dimension 6, at the procedure level, we use:
Dim A (6) as Integer
In the illustration below, A is the matrix name, and the number 6
in parentheses is the upper bound of matrix. The above
declaration creates empty positions in the computer's memory
for an array of 6 elements, with sequence numbers 0 to 5. The
numbers inside the parentheses are called the locations of the

elements of the array. The figure shows the memory location of


matrix A )6(.

Program 16: Write a program to find the elements sum of the


matrix A(5), type single. Print the sum and the value of the
number inside the list box?
Solution:

Dim A(5) As Single, Sum as single


For i = 0 To 4
A(i) = Val(InputBox("enter the number",
"A(i)"))
Next
For i = 0 To 4
Sum = Sum +A(i)
Next
List1.AddItem "Numbers"
For i = 0 To 4
List1.AddItem A(i)
15
Next
List1.AddItem "Sum=" & sum

Project 17: Write a program to finds largest element array.


Show the largest element in message box?
Solution:

Private Sub Command1_Click( )


Dim A(5) as Double
Max = 0
For i = 1 To 5
A(i) = InputBox("A(I)")
If A(i) > = Max Then
Max = A(i)
End If
Next
MsgBox ("max=" & Max, 2,"contenw" )
End Sub

Project 18: Write a program that sorts and prints negative


numbers from positive numbers in the array for a group of
numbers chosen by the student‫؟‬
Solution:

Private Sub Command1_Click


Dim A(i) as Integer
For i = 1 To 10
A(i) = InputBox("inter the number")
If A(i) > 0 Then
List1.AddItem A(i) & " positive "
Else
List1.AddItem A(i) & " negative "
End If
Next
End Sub
16
Project 19: Write a program to find the average of numbers that
are divisible by 5 without a remainder for the numbers from 1 to
100 Use Do-while?
Solution:

Private Sub Command1_Click


Dim n, I, Sum As Integer
n=0
I = InputBox("I "(
Do While I < =100
Sum = Sum + I
I=I+5
n=n+1
Loop
Av=Sum / n
List1.AddItem "Av= " & Av
End Sub

Project 20 : Write a program to calculate the value of z where


z = (x ^ n) knowing that x is any integer that is assigned and
raised to the power n whose values are limited to (1, 2....3).
An error message is displayed if n is given More than 3 or less
than 1?
Solution:

Private Sub Command1_Click)(


Dim x, z, n As Integer
x = Val(InputBox("enter variable x", "x"))
n = Val(InputBox("enter power", "n"))
Select Case n
Case 1
z=x
Case 2
z=x^2
Case 3
z=x^3
17
Case Else
MsgBox ("ERROR - Please try again ",1, "Go On")
End Select
Print "z=" & z
End Sub

Project 21: Write a program that can distinguish the type of


integer positive or negative, where a text message is displayed
to the user informing him the entered number type?
Solution:

Private Sub Command1_Click )(


Dim A As Double
A =val( InputBox (" inter the number"))
If A > 0 Then
x = MsgBox("A Is Positive", 1, " Continue")
Else
x = MsgBox("A Is Negative", 1, " Continue ")
End If
End Sub

Project 22: Write a program that can sort a specific group of


integer numbers into two separate groups, positive and negative,
each group within a list box where a text message is displayed to
the user with the type of each number?
Solution:

Private Sub Command1_Click)(


Dim i As Integer
For i = -5 To 10
If i >= 0 Then
List1.AddItem i & " is positive "
Else
List1.AddItem i & " is negative "
End If
Next
18
End Sub

 Change the input method through the inputbox and choose


numbers
 Change the conditional statement to find even and odd
numbers

Project 23: Write a program to write the elements values and


its square for the array X and display the results on list box?
Solution:

Private Sub Command1- Click)(


Dim X(5) as Integer
For i = 1 To 5
X (i) = val(InputBox ("x"))
y= X (i) ^ 2
tt = "x=" & X (i) & " Sqr( X (i)) =" & y
List1.AddItem tt
Next
End Sub

19
2-Dimension (2-D) Arrays

Arrays can be have multiple dimensions. A common use of


multidimensional matrices is to represent number values of data
arranged in rows and columns. The symbol A (I, J) refers to
matrix A in 2-D array. Other letters can be given to denote rows
and columns, such as M, N or any other letters. The figure
represents the 2-D matrix as a table, where I stands for the rows
number and J stands for the columns number, as shown in the
figure:
Dim A (I, J) As Integer

A matrix is called a square array when the rows number is equal


to the columns number. Square arrays have the following
properties:

1. The Main Diagonal: it is a set of matrix elements joining


between the first element in the first row and the last element
in the last column, in other words it is the matrix elements
when (I = J).
2. The Secondary Diagonal: It is a set of matrix elements
connecting between the last element in the first row and the
20
last element in the last row, in other words it is the matrix
elements when (I+J=I+1) or (I+J=J+1).
(1,1) (1,2) (1,3) (1,4)

(2,1) (2,2) (2,3) (2,4)

(3,1) (3,2) (3,3) (3,4)

(4,1) (4,2) (4,3) (4,4)


Secondary Diagonal Main Diagonal

3. Upper Triangle: The set of elements located above the main


diameter, or in other words (J > I).
4. Lower Triangle: The set of elements located below the
secondary diagonal, or in other words (J < I).

(1,1) (1,2) (1,3) (1,4)

(2,1) (2,2) (2,3) (2,4) Upper Triangle

Lower Triangle (3,1) (3,2) (3,3) (3,4)

(4,1) (4,2) (4,3) (4,4)

Project 24: Find the sum of the main diameter elements and
print them on the list box or another tool such as a picture box?
Solution:

Private Sub Command1_Click)(


Sum = 0
Dim M(3, 3) As Double
For i = 1 To 3
For j = 1 To 3
M(i, j) = Val(InputBox("inter M(i,j"))
21
Print M(i, j);
If i = j Then
Sum = Sum + M(i, j)
List1.AddItem M(i, j)
End If
Next
Print
Next
List1.AddItem "Sum= " & Sum
End Sub

 Change the conditional statement )If I + J = 5 Then( to find


the secondary diagonal elements, and print results and sum
using another tools.
 Change the conditional statement (If J > I Then) to find the
upper triangle elements, print results and sum using another
tools.
 Change the conditional statement (If J > I Then) to find the
lower triangle elements, print results and sum using another
tools.

Project 25: Write a program to print a matrix and find the sum
of the elements of any row or column of the matrix?

Private Sub Command1_Click( )


s=0
Dim a(3, 3)
For i = 1 To 3
For j = 1 To 3
a(i, j) = Val(InputBox("inter a(i,j)")
Print a(i, j) ;
If i = 2 Then '(change this condition for any row or column)'
s = s + Val(a(i, j))
End If
Next
Print
22
Next
Print "Sum= " & s
End Sub
HW:
1. Change i in conditional statement to print another row.
2. Change i in conditional statement with j to print the columns
sum.
3. Write a program to input matrix A (2, 2), and print elements
sum for the matrix on the form?

Project 25: Write a program to print the unit matrix, i.e. all the
elements of the principal diagonal are 1 and the other elements
are zero?
Solution:

Private Sub Command1_Click


Dim a(3, 3)
For i = 1 To 3
For j = 1 To 3
If i < > j Then
a(i, j) = 0
Else
a(i, j) = 1
End If
Print a(i, j) ;
Next
Print
Next
End Sub

23
Project 26: Write a program to print the sum of the elements of
any matrix?
Solution:

Private Sub Command1_Click)(


Dim a(2, 2(
S=0
For I = 1 To 2
For J = 1 To 2
a(I, J) = InputBox("a(I,J "))
Print a(I, J) & Space(4) ;
S = S + Val(a(I, J((
Next
Print
Next
Print "Sum= " & S
End Sub
Project 27 : Write a code program to draw the figures below?

(a)Scale (0, 0)-(10, 10)


FillStyle = 0
FillColor = vbYellow
Circle (5, 5), 3, vbBlack
FillColor = vbBlack
Circle (4, 4), 0.25, vbBlack
Circle (6, 4), 0.25, vbBlack
'Drow mouth
DrawWidth = 3
pi = 3.14159
Circle (5, 5), 1.5, vbRed, 225 * Pi / 180, 315 * pi / 180

(b)Scale (0, 0)-(10, 10)


FillStyle = 0
FillColor = vbYellow
Circle (5, 5), 3, vbBlack
'Drow eyes
FillColor = vbBlack
24
Circle (4, 4), 0.25, vbBlack
Circle (6, 4), 0.25, vbBlack
'Drow mouth
DrawWidth = 3
pi = 3.14159
Circle (5, 8), 1.5, vbRed, 45 * pi / 180,
135 * pi / 180
'Drow nose
Line (5, 4.5)-(5.5, 5.5)
Line (5, 4.5)-(4.5, 5.5)
Line (4.5, 5.5)-(5.5, 5.5)

Project 28: Calculate and draw the slope of relationship


between current and voltage:
V
a) Final resistance ( R = ) using the values that are given in
I
the following table. V volts 10 20 30 40
Dim V(4) As Integer, I(4) I amp 10 20 30 40
As Integer
sumV = 0
sumI = 0

For k = 1 To 4
v(k) =
Val(InputBox("enter v"))
sumV = sumV + v(k)
Next
sumV
MeanV =
4
For k = 1 To 4
I(k) = Val(InputBox("enter i"))
sumI = sumI + I(k)
Next
sumI
MeanI =
4

25
MeanV
R =
MeanI
Text1.Text = CStr(R)
b) Draw the graph between the current and voltages.
Scale (0, 0)-(100, 100)
PSet (10, 10)
PSet (20, 20)
PSet (30, 30)
PSet (40, 40)
Line (10, 10)-(20, 20)
Line (20, 20)-(30, 30)
Line (30, 30)-(40, 40)
End Sub

HW: Give values less than 100 and greater than 0 and specify
the dimensions of the form by instructing scal?

Project 29: Write a program to draw circles and squares starting


from the center of the form and growing with the time that is
determined by the Timer tool, noting that it is possible to stop
execution by the command bottom?
Solution:

Private Sub Command1_Click()


If Timer1.Enabled = True Then
Timer1.Enabled = False
Else
Timer1.Enabled = True
End If
End Sub

Private Sub Form_Load()


Scale (0, 0)-(100, 100)
End Sub

26
Private Sub Timer1_Timer()
Static I As Integer
FillStyle = 2
FillColor = QBColor(5)
I=I+1
'Circle (50, 50), I, vbBlue
'Line (50 - I, 50 - I)-(50 + I, 50 + I), vbRed, B
Line (I, I)-(I + 10, I + 10), RGB(50, 200, 0), B
If I = 50 Then
I=0
Form1.Cls
End If
End Sub

Note: One of the three lines indicated in the previous code is


executed, and the start and stop can be controlled by Stop \ Start,
and the screen is cleaned after stopping by the clear button

Project 30: Write a program that works with a system similar to


the movement of the moon around the earth, where the
execution achieve using command bottoms, the movement can
be stopped using another button?

Private Sub Command1_Click()


If Command1.Caption = "Right" Then
Command1.Caption = "left"
Else
Command1.Caption = "Right"
End If
End Sub

Private Sub Command2_Click()


Timer1.Enabled = Not (Timer1.Enabled)
End Sub

27
Private Sub Timer1_Timer()
Dim i As Integer
Static j As Integer
For i = 0 To 9
Image1(i).Visible = False
Next i
Image1(j).Visible = True
If Command1.Caption = "Right" Then
j=j+1
Else
j=j-1
End If
If j = 10 Then j = 0
If j = -1 Then j = 9
End Sub

Not: so that an image is placed in the center of the form


representing the earth, and 10 or more copies of a second image
are placed at equal distances around the first image and with the
passage of time specified by timer tool, as a result of code
executing one of the images is display and the rest images are
disappear.

28
References:
1. “Programming with Microsoft ® Visual Basic®2017”, Eighth Edition
Printed in the United States of America Print Number: 01 Print Year:
2017© 2018 Cengage Learning.
2. “Introduction visual Basic 6.0”, 1stedition, 2013 Gary Haggard, Wade
Hutchison & Christy Shibata & bookboon.com.
3. Visual Basic for Electronics Engineering Applications. Copyright
1999 – 2005 Vincent Himpe. The author recognizes the copyrights,
trademarks and registered trademarks of all products mentioned in this
book. Initial edition 5-9-1999. Second revised edition 09-03-2002 E-
mail: vincent_himpe@hotmail.com.
4. "Windows Programming Microsoft Visual Basic 6.0" Chapter One
University of Baghdad Collage of Education For Pure Science Dep.of
Computer Science Third Stage M.Sc. Lubab Ahmed
5. “Course Notes for: Learn Visual Basic 6.0” , © Lou Tylee, 1998
KIDware 15600 NE 8th, Suite B1-314 Bellevue, WA 98008 (206)
721-2556 FAX (425) 746-4655.
6. ‫ تاليف احمد صادق‬, "‫"اكواد الفجوال بيسك‬
7. 2008 ‫"فجوال بيسك خطوة بخطوة" تاليف احمد جمال خليفة‬
8. ‫تاليف انس ابراهيم محفوظ‬, "6 ‫"تميز مع فجوال بيسك‬
9. ‫" تاليف محمد عبد الناصر الخطيب‬6 ‫"فجوال بيسك ستوديو‬

29

View publication stats

You might also like