Monday, October 1, 2012

VB experiment Help(Mech)


Experiment No. 1.

1.      Write a program using VB to make a calculator.


Code:

Dim op As String
Dim n1, n2, n3, flag As Single

Private Sub Command1_Click(Index As Integer)

Text1.Text = Text1.Text + Command1(Index).Caption
End Sub

Private Sub Command2_Click()
n1 = Val(Text1.Text)
op = "+"
Text1.Text = ""
End Sub

Private Sub Command3_Click()
n1 = Val(Text1.Text)
op = "-"
Text1.Text = ""

End Sub

Private Sub Command4_Click()
n1 = Val(Text1.Text)
op = "*"
Text1.Text = ""

End Sub

Private Sub Command5_Click()
n1 = Val(Text1.Text)
op = "/"
Text1.Text = ""
End Sub

Private Sub Command6_Click()
n2 = Val(Text1.Text)
If op = "+" Then
n3 = n1 + n2
ElseIf op = "-" Then
n3 = n1 - n2
ElseIf op = "*" Then
n3 = n1 * n2
ElseIf op = "/" Then
n3 = n1 / n2
End If
Text1.Text = n3
'flag = 1

End Sub

Private Sub Command7_Click()
Text1.Text = ""
End Sub

Private Sub Command8_Click()
Dim str As String, i As Integer
str = Text1.Text
 i = InStr(1, str, ".", 1)
 If i > 0 Then
 i = 0
 Exit Sub
End If
Text1.Text = Text1.Text + Command8.Caption
End Sub

Form:





 

Experiment No. 2.


2.      Create a visual basic project that will either convert U. S. dollars into a foreign currency or convert a foreign currency into U. S. dollars.
                  The foreign currencies and their U. S. dollar equivalents are:
                  1 U. S. dollar = 0.6 British pounds
                                            1.4 canadian dollars
            Dutch guilders
6.8 Frech francs
2.0 German marks
2000 Italian lira
100 Japanese yen
9.5 Mexican pesos
1.6 Swiss francs
            Your project should include two option buttons within a frame, to select either U. S. to foreign conversion or foreign to U. S. conversion. Nine additional option buttons should be placed within another frame to select the particular foreign currency. Use a text box to specify a given amount of money in the source currency, and another text box to display the equivalent amount of money in the target currency. Include an appropriate set of labels for each conversion type.

Code:

Dim cntCurrency As Integer

Private Sub Form_Load()
    opt(0).Value = True
    optCurrency(0).Value = True
End Sub

Private Sub opt_Click(Index As Integer)
    Call ConvertMoney
End Sub

Private Sub optCurrency_Click(Index As Integer)
    cntCurrency = Index
    Call ConvertMoney
End Sub

Private Sub ConvertMoney()
If opt(0).Value = True Then
    Text2 = Val(Text1.Text) * Val(optCurrency(cntCurrency).Tag)
Else
    Text2 = Val(Text1.Text) / Val(optCurrency(cntCurrency).Tag)
End If
End Sub





Private Sub Text1_Validate(Cancel As Boolean)
    Call ConvertMoney
End Sub


Form:



























Experiment No. 3.
3.      Write a Visual basic program to perform the following task.
i)                    Reverse a string.
ii)                  Determine whether the given string is palindrome or not.
iii)                Find out whether given no is prime.
iv)                Find the Largest three
Code:

Dim str, rev As String
Dim i, l, r, n As Integer

Private Sub Command1_Click()
str = Text1.Text
l = Len(str)
For i = l To 1 Step -1
rev = rev + Mid(str, i, 1)
Next i
MsgBox "  " & rev, vbOKOnly, "Reverse string"
End Sub
Private Sub Command2_Click()
If StrComp(str, rev) = 0 Then
MsgBox "The String is palindrom", vbOKOnly, "Result"
Else
MsgBox "The String is not palindrom", vbOKOnly, "Result"
End If
End Sub

Private Sub Command3_Click()
n = Val(Text1.Text)
For i = 2 To (n - 1)
    If (n Mod i = 0) Then
    MsgBox "Not a prime no.", vbOKOnly, "Result"
    End If
Next i
If n = i Then
    MsgBox "prime no.", vbOKOnly, "Result"
End If
   
End Sub

Form:

Experiment No. 4.

4.      Write a Visual basic program to perform the following task.
i)                    Find the power of a given number.
ii)                  Determine that weather a given number is Armstrong.
iii)                Display the table of a given number
iv)                Arithmetic operations.


Code:

Private Sub Command1_Click()
Dim i, n1, n2, pow As Integer
n1 = Val(Text1.Text)
n2 = Val(Text2.Text)
pow = 1
For i = 1 To n2
pow = pow * n1
Next i
'Print "" & n1, "raise to" & n2, " is" & pow, vbOKOnly, "Result"
Print n1; "raise to"; n2; "is "; pow

End Sub

Private Sub Command2_Click()
Dim n1, i, l, arm As Integer
n1 = Text1.Text
l = Len(n1)
arm = 0
For i = 1 To l
arm = arm + (Val(Mid(n1, i, 1)) ^ 3)
Next i
If arm = n1 Then
Print "armstrong"
Else
Print "Not armstrong"
End If
End Sub




Experiment No. 5.
5.      Create a visual basic alarm clock that utilizes the timer control. Use option buttons to turn the alarm on and off. Specify the wake-up time using a text box.

Code:

Option Explicit
Dim cnt
Dim sec As Integer, hrs As Integer, min As Integer
Private Sub Form_Load()
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Call UpdateTime
End Sub

Private Sub UpdateTime()
    If sec = 60 Then
        sec = 0
        min = min + 1
        If min = 60 Then
            min = 0
            hrs = hrs + 1
        End If
        If hrs = 24 Then
            hrs = 1
        End If
    Else
        sec = sec + 1
    End If
    Text1.Text = hrs & ":" & min & ":" & sec
End Sub

Form:






Experiment No. 6.
6.      Write a visual basic program that will rearrange a list of names into alphabetical order. To do so, enter the names into a one-dimensional string array, with each element represent one complete name. The list of names can then be alphabetized in the same manner that a list of numbers is rearranged from smallest to largest.
Code:

Option Explicit
Dim s() As String

Private Sub CmdSort_Click()
Dim i As Integer
Dim j As Integer
Dim temp As String
ReDim s(10)
For i = 1 To 10
    s(i) = InputBox("Enter the value")
Next
For i = 1 To 10
    List1.AddItem s(i)
Next

For i = 1 To 10
    For j = i + 1 To 10
        If UCase(s(i)) > UCase(s(j)) Then
            temp = s(i)
            s(i) = s(j)
            s(j) = temp
        End If
    Next
Next
For i = 1 To 10
    List2.AddItem s(i)
Next
End Sub
Form:




Experiment No. 7.
7.      Create a visual basic project that will allow the user to select the name of a country from a list and then display the corresponding capital, and vice versa.

Code:

Private Sub Command1_Click()
If List1.Text = "india" Then
MsgBox "Delhi", vbOKOnly, "Capital"
ElseIf List1.Text = "pakistan" Then
MsgBox "karachi", vbOKOnly, "Capital"
End If
End Sub

Form:







No comments:

Post a Comment

Lecture PPTs of MIS , Module 4 to Module 6

 Module 4 Social Computing (SC): Web 3.0 , SC in business-shopping, Marketing, Operational and Analytic CRM, E-business and E-commerce B2B B...