Is not case sensitive
Logics and operations are unified
It support the use of objects
Can create executable files.
Statements are terminated by keyword "End if"
Data Types;Are set of data with values having predifined characteristics.
Variables;Is a named memory location used to store values but keep on changing during program execution
Constants;Is a named memory location that stores values but do not change during program execution.
Identifies;Are names given by. Programmers to objects in a program.
It should be less than 255 characters.
It should not be a keyword or reserved word.
It should always begin with an alphabet.
It should not contain blank spaces.
It should be unique within given slope.
Visual basic is said to be event driven because;
All or majority of the code happens as a result of something else,which is an event.
A message box has three parts namely;
Prompt;Is a message of a message box.
Tittle;Is the tittle of the message box.
Helpfile and Context;Is where in a helpfile a user can find help from that message box.
Parts of the input box:
title Prompt;This is the message that input box has.It tells the user what the program wants them to input.
s:
Data Types;Are set of data with values having predified characteristics.
Variables;Is a named memory location used to store values but keep Post titlechanging during program eecution
Constants;Is a named memory location that stores values but do not change during program eecution.
Identifies;Are names given by. Programmers to objects in a program.
Data Types in Visual Programming
Avariable:
Is a named memory location that holds data
It can hold only one data type i.e String,Integer.A program may have as many variables as you need but it has to be declared before being used.We use Dim statement to declare variables.
Formattof declaring variables using a Dim
Dim Var Name As Data type i.e
Dim curcost As currency.
Dim strsurname as String
Function;Is a segment of codes that accepts one or more arguments and return a 0 results.
Visual basic include many build in functions.
Intrinsic functions;Some perform basic mathematical task while others manipulate string data such as converting text to upper or lower case letters.
Arguments;Is a value passed to a function so that a function has data to work with.
N/BProperty of a label are;Name,Caption and color.Label caption(object property)="Welcome to visual basic"(option property).
An object;Is anything that appears on the screen.It has a set o properties and each property has a set of values.
Events;Are things that happen on the screen.
Procedure;Is a group of statement design to perform a specific task.
Quiz
Design a form containing 2 text box so that when you click on command button copy,the text is copied from 1st to 2nd text box.
Hint 1label,1 text box and 1 command.
Private sub command-click()
Text2.text=Text1
Texr2.font size=28
End sub.
Option button;
It is used only as a group of buttons.When the user select one of them,the others are deselected automatically.The option button usually takes click event.
Quiz
Design a form with 3 option button,Red,Green and blue,such that when we click on option button, the color of the form colors to red,green and blue respectively.
Hint;
3 option buttons with 3 labels.
Form.back color=vb green
End sub.
Private sub option-click()
Form1.back color=vb red
End sub.
Private sub option-click()
Form1.back color=vb blue
End sub.
De sine a form with one text box and three check boxes such that when you click on the check boxex the following is done.Change text to bold,italics and underline.
text.font bold=check2.value
End sub.
Private sub check2.click()
Text.font italic=check2.value
End sub.
Private sub check3.click()
text.font underline=check3.value
End sub.
Timer
It returns the time in milliseconds.It may be used to measure execution time of codes(program efficiency)
Design a form to display "Aplied Science"such that when you click on "command button"start the color of the applied science change randomly every second.
Hint;Timer,label,command button.
The function "Cint"is used to convert to integer and "Rnd" is used to generate Armando number in a range(0-1).
Solution;
t=Rnd*15
label1.fore color=qbcolor(Cint(t))
End sub.
Private sub command1.color check()
timer.Enabled=True
End sub.
Write a program to move text excellent from textbox to msgbox and change the color of the text.After click on command button display.
Hint;
1 textbox,1 command button.
text1.back color=qbcolor(g)
text1.text=" "
End sub.
Description
Absolute(abs)(x) Absolute of x
Squre(sqr)(x)Squreroot of x
Integer(int)Integer of x
Exponential(exp)(x) Exponential of
Fixation(x)Take integer part
Sin (x)Cos(x)Tan(x)=0Trigometric functions
Log(x)Natural logarithms
Len(x)Number of characters of variable x
Lcase(x)Change text x to upper case
Ucase(x)Change text x to lower case
Cint(x)Convert x to integers
ClongxConvert x to long
Cdbl(x)Convert x to double precission
Cstr(x)Convert variable x to string
val(x)Convert string x to numerical value.
Write a program to enter any text and compute its length.
Hint;
1 label,1 textbox and 1 command button.
Private sub command1_click()
S=input box("Enter String")
L=len(s)
Text1.text=(str(l)
End sub.
Write a program to add and subtract 2 integer numbers.Use messagebox for outputting.
Hint;
2 labels,2 text boxes and 2command buttons.
Private sub command1_click()
x=val(text1.text)
y=val(text2.text)
z=x+y
msgbox("Addition results="&z)
End sub
Private sub command2_click()
x=val(text1.text)
y=val(text2.text)
z=x-y
msgbox("subtraction results="&z)
End sub
dim x,y,z AS integer
Private sub command1_click()
x=val(text1.text)
y=val(text2.text)
z=x+y
Text3.text=z
A program that compute a function sine,cosine,integer value,squre and the absolute value.
Hint1 text box and 6 command buttons.
Private sub command1_click()
x=val(text1.text)
y=Abs(x)
Text1.text=Cstr(y)
End sub
x=val(text1.text)
y=sqr(x)
text1.text=Cstr(y)
End sub
Private sub command3_click()
x=val(text1.text)
y=int(x)
text1.text=Cstr(y)
End sub
Private sub command4_click()
x=val(text1.text)
y=sin(x*3.114159/180)
text1.text=Cstr(y)
End sub
A program that allows the name of a student and two marks of any subjects by input box then compute the average and display the name and average in two labels.
Hint
two labels and one command button.
Private sub form1_load
x=input box("Enter your name")
y=input box("Enter the 1st marks")
z=input box("Enter the 2nd marks
Private sub command1_click
Av=val(y0/2+val(z)/2
Lbel2.caption=Av
End sub.
Hint
Area=1/2*b*h
Two labels,one text box and one command button.
Private sub command1_click()
b=val(text1.text)
h=val(text1.text)
a=0.5*b*h
msgbox("area="&a)
End sub.
Ladel.caption=Time value(now)
Label2.caption=Date value(now)
End sub
MENU AND MENU EDITOR
Menu is the bar at the top of the form.The form is displayed without the menu but the user can add it.This menu could be included in forms using menu editor.Menu Editor.
There are three ways to use the menu editor,
Menu icon in the tool bar
CTR + E
Tools-Menu Editor.
A menu editor box appear as where we can add items,remove items,move items down and move items up.
Standard tool bar-Command1
........Open new file-cmd2
....SAve a document-cmd3
Formatting toolbar-cmd4
Font size
There are of two types;
If statement
Select case
Simple structure if...Then
It is used for running one programming statement only if the required condition is satisfied.
Format;
If condition The statement
Program that allows marks of a student then quote pass if successful
Hint;
Two labels,two text box and one command.
Private sub command_click()
x=Cint(text1.text)
if x>=50 Then
text2.text="pass"
End sub
Format
ifconditionThen
Statement
Else
It is used for running many programming statements if the required condition is met.and another programming statement (after else)if the required condition is not satisfied.
Program that allows marks of a student the prints pass if successful and prints fail other wise.
Private sub command1_click()
x=Cint(text1.text)
if x>=50 Then,br>text2.text="pass"
text2.fontsize="18"
text2.forecolor=vbred
Else
text2.text="fail"
End if
End sub
Program that allows 5 marks of a student,computes the average and displays if average is greater than 50 pass and if below dis[plays fail on a message box.
Private sub Form load_click()
a=inputbox("English")
b=inputbox("Kiswahili")
c=inputbox("Maths")
d=inputbox("Geography")
e=inputbox("Physics")
End sub
Private sub command1_click()
xsum=a+b+c+d+e
Average=sum/5
if sum>=50 Then
msgbox"pass"
Else
msgbox"fail"
End if
End sub
Are used when we have many conditions to be satisfied.
A program that allows the user name and displays the message Hello 3 times.The first one for Ann,2nd Rachael and 3rd any user as a Guest.
Hint1 Label,1 Textbox and 1 command.
private sub command1_click()
x=text1.text
if x="Ann" Then
msgbox="Hello Ann"
Else if
x="Rachael" Then
msgbox="Hello Rachael"
Else
msgbox"Hello Guest"
End if
End sub.
A program that classify any entered number according to its sign and display the phrase -ve number when the number is -ve and positive when it is,otherwise display the phrase neither -ve nor +ve.
private sub command1_click()
x=val(text1.text)
if x>0 Then
msgbox"positive value"
Else if x< 0 Then
msgbox"Negative value"
Else
msgbox"positive or negative"
End if
End
End sub
if condition Then
If condition Then
Statements
End if
End if
A program that accepts two numbers and compute maltiplication and division operations using option buttons with the display pha se"Legal division operation".When the denomenator is 0.
private sub command1_click()
a=val(text1.text)
b=val(text2.text)
if option1.value Then
c=a*b
text3.text=Cstr(c)
Else if
b<>0 Then
C=a/b
Text3.text=Cstr(c)
Else
text3.text="Legal devision operation"
End if
End if
End sub
Select statement
The program that print days of the week when we enter its number.Hint
1 label,1 textbox and 1 command buttton.
private sub command1_click()
x=Cint(text1.text)
select case x
case1
msgbox"sunday"
case2
msgbox"Monday"
Case3
msgbox"Tuesday"
case4
msgbox"Wednesday"
case5
msgbox"Thursday"
case6
msgbox"Friday"
case7
msgbox"Saturday"
End select
End sub.
A program that give a variation of marks as follows.use select case.
90 to 100-Excellent,80 to 89-verygood,70 to 79-Good,60 to 69-medium,50 to 59-pass and 0 to 49-fail.Case Else....out of range.
Hint1 label,1 textbox and 1command button.
private sub command1_click()
x=Cint(text1.text)
select case x
case 90 to 100
msgbox"Excellent"
case 80 to 89
msgbox"Verygood"
case 70 to 79
msgbox"Good"
case 60 to 69
msgbox"medium"
case 50 to 59
msgbox"pass"
case 0 to 49
msgbox"fail"
case Else
msgbox"out of range"vb critical.
End select
End sub
program that solves the quadratic equation.
Dim x As integer
a=Cint(text1.text)
b=Cint(text2.text)
c=Cint(text3.text)
if Cint(text2.text)<0 br="" then="">x=-b-(b*b-4*a*c)*0.5/2*a
Else if Cint (text2.text)>0 then
x=-b+(b*b-4*a*c)*0.5/2*a
text4.text=Cint(x)
End If
End sub.0>
Form with the text box.
select statements used so that when users enter G,B,R&Y the form colors to green, blue, red, and yellow respectively
Private sub command1_click()
select case color
case color G
Form.back color=VB Green
case color B
Form1.backcolor=VB blue
case color R
form1.backcolor=VB red
case color y
form1.backcolor=Vb yellow
End select
End sub.
Program that solves the surface area of a cone.
SA=pi*r*h+pi*r*r
Dim r,s,a As single
r=val(text1.text)
s=val(text2.text)
A=(pi*r*s)+(pi*r*r)
msgbox("Surface area="&A)
End sub.
Arrays.
An array is a list of variables all with the same type and name.We need to declare an array of varriables instead of using a variable for each item.
Declaring ArraysPublic statement;declares an array that can be used throut.
DimDeclares an array that can be used only in local procedures
Loop Statements
Counter loopsApplies proggramming statemments for a fixed number of time using:counter statements
counter(for...next)statement
Programe that prints Hello words 5 times.
private sub command1_click()
For i = 1 to 5
print"hello"
Next i
End sub
A programe that prints even numbers from 1 to 10.
private sub command_1click,br>For i=2 to 10 step2
print i
next i
End sub.
The variable values that we use as counter must be an integer or long value.
if we dont determine the step value then the asigned value is 1.If the final value is smaller than the initial value,then the step value must be negative
Conditional loop
do while
Do until
The loops report program statements according to specific conditions
Do.....while
her statements wil be implimented and repeated whenever the conditions is satisfied
General formatt
Do..while...condition
statement
loop
A programe that prints Hello 5 times with its numbering using Do.....while loop
private sub command1_click()
i=1
Do while i<=5
print "Hello";i
i=i=1
loop
End sub
i=2
Do While i<=10
print i
i=i+2
loop
End sub
Do....Until loop
it is implimented and used whenever the condition is not satisfied.i.eloop will stop when condition is satisfied.
do until condition
statement
loop
A program that prints hello 5 times with its numbering using d....until.
i=1
Do until i>5
print"Hello";i
i=i+1
loop
end sub
A program that prints the multiplication tables.
private sub command1_click()
For i=1 to 10
For j=1 to 10
p=i*j
print i;"*";j;"=";p
Next j
Next i
End sub.
dimcount(100 to500) as integer
This declares an array that consists of the 1st element starting from count 100 and ends at count 500
a program that accepts data in an inputbox and displays the entries in the form itself.the programe should allow the user to enter 10 names each time he clicks on the start button.
hint;2 command button
dim num as integer
private sub Add name(0)
for num=1 to 10
student name(num)=inputbox("Enter student name")
if student name(num)<>" "Then
Form1.print student name(num)
Else
End
End if
Next
End sub
private sub command1_click()
form1.cls
addname
private sub command2_click()
End
End sub.
a program that accepts entries through an inputbox and displays the item in a list box.
Hint.list box and 1 command button.
Dim student name(0)As string
dim num as integer
private sub add name()
For num=1 to 10
studentname(num)=inputbox("Enter student name")
List1.Add item studentName(num)
next
End sub
private sub command_click()
form1.Cls
addname
End sub
A program that prints the word"Welcome Home" ten times,the first with ordinary size and color then make color change and size bigger at each time.
solution
Private sub command1_click()
print"Welcome Home"
For i=1 To 10
Fontsize=10+i
Forecolor=QBcolor(i)
print"Welcome Home"
Next i
End sub.
A program that prints multiples of 5 to 50.
solution.
Private sub command1_click()
i=5
Do until i>50
print i
i=i+5
Loop
End sub
A program that compute the area of a circle.
solution
Dim a,r as string
private sub text1_change()
Command1_Enabled=True
End sub
private sub command1_click()
r=val(text1.text)
a=r^2*p
msgbox("area="&a)
text1.text=" "
text1.set focus
End sub.
A program tha finds the summation of under determined number of positive numbers such that the program will be stopped when we enter negative numbers.
Solution
private sub command1_click()
sum=0
x=val(inputbox("Enter x","summation"))
Do While x>=0
sum=sum +x
x=val(inputbox("Enter x","summation"
Loop
msgbox(Cstr(sum)
End sub
A program that finds out the summation of the numbers from 5 to 15.Solution.
Dim i ,sum as integer
private sub command1_click()
sum=0
For i=5 To 15
sum=sum+i
Next i
Label.caption="sum="&Cstr(sum)
End sub.
A program t6ghat finds out the summation of 10 numbers.Solution.
Dim i as Integer
Dim x,sum as double
private sub command1_click()
sum=0
For i=1 To 10
x=val(inputbox("Enter number"))
sum=sum+x
Next i
Label1.cation="sum="&Cstr(sum)
End sub.
A program that finds out the average of n numbers.Solution.
Dim i as integer
Dim x,sum as double
private sub command1_click()
i=1:sum=0
n=Cint(text1.text)
Do While i<=n
x=val(inputbox("Enter number")
sum=sum+x
i=i+1
LOOP
Av=sum/n
text2.text=Cstr(av)
End sub.
its fun to have this on web.i like it
ReplyDeleteso great
ReplyDeleteSelect out a custom state of affairs from our large extent of things. Any hazard that you do now not even once more have the best viable estimations to your Custom Packaging holders,
ReplyDelete