2Practicals Visual Basic 6.0 Practical 1: 1. Navigation of Visual Basic Integrated Development Environment The Visual Basic IDE is made up of a number of components Menu Bar Tool Bar Project Explorer Properties window Form Layout Window Toolbox Form Designer Object Browser Code Window First of all, you have to launch Microsoft Visual Basic 6.In the Visual Basic 6 integrated development environment, a default form with the name Form1 will be available for you to work on your new project. Now, double click on Form1, the source code window for Form1 as shown in figure will appear. The top of the source code window consists of a list of objects and their associated events or procedures. In figure, the object displayed is Form and the associated procedure is Load. Vbforswedish.blogspot.com Page 1
Practical 2: Controls And Properties(1): Visual Basic Controls and Properties provide a simple means to quickly create rich interfaces for your Visual Basic applications. Controls on toolbar are shown in the picture: Properties can be set by highlighting the items in the right column then change them by typing or selecting the options available. Program to change shape This code will change the shape to a circle at runtime. Private Sub Form_Load() Shape1.Shape = 3 Vbforswedish.blogspot.com Page 2
Practical 3: empty. Controls And Properties(2): To calculate and to display the output on the label The two text boxes are used to accept inputs from the user and one of the labels will be used to display the sum of two numbers that are entered into the two text boxes. Besides, a command button is also programmed to calculate the sum of the two numbers using the plus operator. Draw Graphical User Interface 1. Insert two text boxes along with three labels and a command button. 2. As given in the figure. Attach Code 1. Double Click The command button, code window will appear. 2. Then write the following code in the code window. 3. Now run the program. Dim x As Integer, y As Integer x = Text1.Text y = Text2.Text Label3.Caption = Label3.Caption & x + y 4. An example output is given in the figure. Update Properties 1. Change label1.caption as Number1, label2.caption as Number2 and label3. Caption as Sum= 2. Change text1.text and text2.text to Vbforswedish.blogspot.com Page 3
Attach Code Double Click The command Practical 4: Creation of Label, Button and text Boxes: Label: It represents a standard Windows label. It is generally used to display some informative text on the GUI which is not changed during runtime. Textbox: It is used to take it input or display output to the user. Command Button: It is generally used to generate a Click event by providing a handler for the Click event. Print "Welcome " & Text1.Text button, code window will appear. Then write the following code in the code window. Now run the program. An example output is given in the 1.Put a label as label1, a text box as text1 to take input and a command button as command1 Update label1 caption Type your name? go to properties window select Font option, make style BOLD and size 14. Make text1.text = Change the caption of command1 to CLICK Figure 1 Practical 4 Vbforswedish.blogspot.com Page 4
Practical 5: Program that will show you a message "Welcome you are campus 3 student if your roll number satisfies the rule. 3. Place a label on the form. Dim x As Long Dim y As Long x = InputBox("Enter your Roll#?", "Campus Check") y = x \ 1000000 If y = 3 Then MsgBox "Welcome you are campus 3 student" 1. Update label1 caption as Click button 2. Set caption of the button to Press. End If 1. Double Click button. 2. Write the following code in the click 3. Run the program. Vbforswedish.blogspot.com Page 5
Practical _6: Program that will input a number from user and will display weather it is positive, negative or zero? 3. Place a label on the form. 4. Place a text box on the form. 1. Update label1.caption as Enter a number to check? 2. Set button caption to check Nain Markaz-e-Taleem 1. Double Click button. 2. Write the following code in the click 3. Run the program Dim x As Integer x = Text1.Text If x > 0 Then MsgBox "Positive" ElseIf x < 0 Then MsgBox "Negative" Else MsgBox "Zero" End If Vbforswedish.blogspot.com Page 6
Practical _7: Write a program that will take your marks out of hundred and calculate your grade? 3. Place a label on the form. 4. Place a text box on the form. 1. Update label1.caption as Enter your marks to check your grade? 2. Set button caption to Grade 1. Double Click button. 2. Write the following code in the click Nain Markaz-e-Taleem Dim marks As Integer marks = Val(Text1.Text) Select Case marks Case Is > 80 MsgBox "Grade A" Case Is > 60 MsgBox "Grade B" Case Is > 40 MsgBox "Grade C" Case Else MsgBox "Fail" End Select Figure 2 Practical _7 Vbforswedish.blogspot.com Page 7
Practical 8: Write a program that will calculate the factorial of given number using while loop? 3. Place a label on the form. 4. Place a text box on the form. Nain Markaz-e-Taleem 1. Update label1.caption as Enter a number to get factorial? 2. Set button caption to factorial 3. Set text1.text= Figure 3 Practical 8 1. Double Click button. 2. Write the following code in the click Dim n As Integer, i As Integer, fact As Integer n = Text1.Text fact = 1 i = 1 While i <= n fact = fact * i i = i + 1 Wend MsgBox "Factorial" & fact Vbforswedish.blogspot.com Page 8
Practical 9: Write a program that displays first five odd numbers using FOR Next loop? 4. Set button caption to Show Odd 3. Double Click button. 4. Write the following code in the click Nain Markaz-e-Taleem Dim number as integer Print First Five ODD numbers: For number=1 to 10 step 2 Print number Next Figure 4 Practical9 Vbforswedish.blogspot.com Page 9
Practical 10: Add menus in an application. Change Background colors through menu options? Draw GUI and update properties: 2. Press CTRL+E or select Menu Editor from TOOLS menu to start a menu editor. 3. Enter Colors in caption and mnucolor in Name. The name of the menu will appear in lower box of menu editor. 4. Click next to add another menu. 5. Enter RED in caption field and mnured in name field and then do the same for Green and Yellow. 6. set shrot cut Ctrl+r for red, Ctrl+g for green and Ctrl+y for yellow. 5. Click the menu Colors->Red button. 6. Write the following code in the click Nain Markaz-e-Taleem Private Sub mnugreen_click() Form1.BackColor = vbgreen Private Sub mnured_click() Form1.BackColor = vbred Private Sub mnuyellow_click() Form1.BackColor = vbyellow Vbforswedish.blogspot.com Page 10
Practical 11: Write a program that will take your year of birth and return your age in years using function procedure? 3. Place a label on the form. 4. Place a text box on the form. 1. Set button caption to Age. 2. Set the caption of albel1 to Enter your Date of birth? 3. Set text1.text=. 4. Set format of text1.text to DATE. 1. Double Click button. 2. Write the following code in the click Dim birth As Date Dim yr As Integer birth = Text1.Text yr = age(birth) MsgBox "Your age is= " & yr Private Function age(b As Date) As Integer Dim cd As Date cd = Date age = DateDiff("yyyy", b, cd) End Function Vbforswedish.blogspot.com Page 11