Programming Project with VB



Program 1: InputBox & MsgBox 1
This program shows you the simplest form of the InputBox and MsgBox controls. A number is taken as input and the same is shown in MsgBox.
For further learning go to : 

   
          

Code
Private Sub Form_Load()
   
Dim n as byte
    n = InputBox("Enter a number")
    MsgBox "You entered " & n
End Sub
___________________________________________________




Program 2: InputBox & MsgBox 2
This is another simple example of the InputBox and MsgBox controls. Two numbers are taken through InputBox, and the sum is shown in the MsgBox.





Code
Private Sub cmdEnterValues_Click()
Dim a,b as byte
    a = Val(InputBox("Enter the value of a", "Input value"))
    b = Val(InputBox("Enter the value of b", "Input value"))
    MsgBox "The sum is " & a + b, vbInformation, "Output"
End Sub

__________________________________________________




Program 3: Output with PictureBox
The program shows how to show message in PictureBox. The Print and Cls methods are used in this program. 

             

Code
Private Sub cmdClear_Click()
    picOutput.Cls       'Cls method clears the picture box
End Sub

Private Sub cmdShow1_Click()
    picOutput.Print "Hello World !"
End Sub

Private Sub cmdShow2_Click()
    picOutput.Print "Welcome to Visual Basic !"

End Sub
_____________________________________________________





Program 4: If-Then statement
If-Then statement is illustrated in this program. A number is taken through InputBox and shown using the Print method whether it is greater than 5.


 
             

Code
Private Sub Form_Load()
Form1.Show
Dim a As Integer
a = InputBox("Enter the value of a", "Input")
If a > 5 Then
    Print "The number is greater than 5"
End If

End Sub
_____________________________________________________



Program 5: If-Else statement
This example program is almost same as the previous one. The only difference is that an Else statement has been added to the code.


             

Code
Private Sub Form_Load()
    Form1.Show
    Dim a As Integer
    a = InputBox("Enter the value of a")
    If a > 5 Then
        Print "The number is greater than 5"
    Else
        Print "The number is less than 5"
    End If
End Sub
_____________________________________________________



Program 6: Largest Number
The Largest Number sample program finds out the largest number among three numbers. This program uses If-Else statements.

             
Code
Private Sub cmdCalculate_Click()
    Dim num1 As Integer, num2 As Integer, num3 As Integer
    num1 = Val(txtNum1.Text)
    num2 = Val(txtNum2.Text)
    num3 = Val(txtNum3.Text)
   
    If num1 > num2 And num1 > num3 Then
        lblResult.Caption = num1
    ElseIf num2 > num1 And num2 > num3 Then
        lblResult.Caption = num2
    Else
        lblResult.Caption = num3
    End If
End Sub
_________________________________________________________




Program 7: Word Length
The Word Length sample program calculates the length of a word. The word is given as an input through InputBox and then the length is shown on the form. There's a simpler way to solve this kind of problem but this method in the program  has been applied only to teach you about the Select Case structure.

              

Code
Private Sub cmdStart_Click()
    Dim word As String, n As Integer
    word = InputBox("Enter the word", "Input")
    n = Len(word)
    

    Select Case n
    Case 0
        Print "you have not entered any word"
    Case 1
        Print "This is a 1 letter word"
    Case 2
        Print "This is a 2 letters word"
    Case 3
        Print "This is a 3 letters word"
    Case 4
        Print "This is a 4 letters word"
    Case 5
        Print "This is a 5 letters word"
    Case Else
        Print "The word contains more than 5 letters"
    End Select

End Sub
__________________________________________________________



Program 8: Grade Program
The sample program calculates the obtained marks and percentage of marks according to the specified grade. Select Case statements have been used in this example.

             

Code
Private Sub cmdCalculate_Click()
    Dim s As String
    s = txtGrade.Text
  
    Select Case s
    Case "E"
        lblMarks.Caption = "above 90%"
        lblRemarks.Caption = "Excellent"
    Case "A+"
        lblMarks.Caption = "above 80%"
        lblRemarks.Caption = "Very Good"

    Case "A"
        lblMarks.Caption = "above 70%"
        lblRemarks.Caption = "Good"

    Case "B"
        lblMarks.Caption = "above 60%"
        lblRemarks.Caption = "Average"

    Case "C"
        lblMarks.Caption = "above 50%"
        lblRemarks.Caption = "Satisfactory"
    Case "D"
        lblMarks.Caption = "above 40%"
        lblRemarks.Caption = "Poor"
    Case "F"
        lblMarks.Caption = "above 35%"
        lblRemarks.Caption = "Fail"
    End Select

End Sub






counting the letters of a word



Code
Private Sub cmdclear_Click()
lblresult.Caption = ""
End Sub
Private Sub cmdOK_Click()
Dim m As Integer
m = Len(txtword.Text)
lblresult.Caption = m
End Sub

Code
Private Sub cmdok_Click()
Dim m As Byte
m = Val(txtmarks.Text)
Select Case m
Case 0 To 35
lblresult.Caption = "W"
Case 35 To 55
lblresult.Caption = "S"
Case 55 To 65
lblresult.Caption = "C"
Case 65 To 75
lblresult.Caption = "B"
Case Else
lblresult.Caption = "A"
End Select
End Sub

Do -  Loop
Do-Loop ක්‍රියාත්මක වන ආකාර කිහිපයක් ඇත.

a)   Do While condition(ප්‍රකාශණය)
            Block of one or more VB statements(විධාන එකක් හෝ කීපයක්)
      Loop

b)   Do
            Block of one or more VB statements (
විධාන එකක් හෝ කීපයක්)
      Loop While condition
(ප්‍රකාශණය)


c)    Do Until condition (ප්‍රකාශණය)

              Block of one or more VB statements (
විධාන එකක් හෝ කීපයක්)

       Loop

d)    Do
             Block of one or more VB statements (
විධාන එකක් හෝ කීපයක්)

       Loop Until condition (ප්‍රකාශණය)

විධාන වලට උදාහරණ, x=x+1, y=x+z, print x, x=x*x
ප්‍රකාශණ වලට උදාහරණ N>=10, X<=25, Y=100

Code
  Do While (ප්‍රකාශණය)
           විධාන එකක් හෝ කීපයක්
      Loop

  Do
(
විධාන එකක් හෝ කීපයක්)
      Loop While
(ප්‍රකාශණය)

Do Until (ප්‍රකාශණය)
(විධාන එකක් හෝ කීපයක්)
       Loop

 Do
            (
විධාන එකක් හෝ කීපයක්)
       Loop Until condition
(ප්‍රකාශණය)

Private Sub cmdOK_Click()
Dim number As Integer
number = 1
Do While number <= 10
number = number + 1
Print number
Loop
End Sub
Private Sub cmdOK_Click()
Dim number As Integer
number = 1
Do
number = number + 1
Print number
Loop While number <= 10
End Sub
Private Sub cmdOK_Click()
Dim number As Integer
number = 1
Do Until number >= 10
number = number + 1
Print number
Loop
End Sub
Private Sub cmdOK_Click()
Dim number As Integer
number = 1
Do
number = number + 1
Print number
Loop Until number >= 10
End Sub




http://www.vbtutor.net/lesson9.html

No comments:

Post a Comment