36
1 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science Visual Basic INTRODUCTION TO VISUAL PROGRAMMING Lab # 1 Objective: The objective is to review the basics of Visual programming. Theory Visual Basic (Business All-purpose Symbolic Instruction Code) is a tool that allows you to develop Windows (Graphic User Interface - GUI) applications. How to open Visual Basic 6.0 Step 1: First click on start menu. Step 2: Click on All programs. Step 3: Microsoft Visual Basic 6.0 after that click on visual basic 6.0.

Visual Basic Lab Manual

Embed Size (px)

Citation preview

Page 1: Visual Basic Lab Manual

1 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

INTRODUCTION TO VISUAL PROGRAMMING

Lab # 1

Objective: The objective is to review the basics of Visual programming.

Theory

Visual Basic (Business All-purpose Symbolic Instruction Code) is a tool that allows you to develop Windows (Graphic User Interface - GUI) applications.

How to open Visual Basic 6.0

Step 1: First click on start menu.

Step 2: Click on All programs.

Step 3: Microsoft Visual Basic 6.0 after that click on visual basic 6.0.

Page 2: Visual Basic Lab Manual

2 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Visual Basic will open

New Project

Page 3: Visual Basic Lab Manual

3 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

You must select/open Standard.Exe according to the above figure

Now, you can design a program by adding some tools like button, comobox, textbox, label etc.

Program: 1 Write a program that display the message box of your name.

Solution:

Running a simple VB Program in Visual Basic 6.0

Step 1. First of all you have to select a form1.

Step 2. Now, select a button from toolbox and draw on the form1 that button.

Step 3. Now change the caption of form1 and button1,

Form1 = MessageBox

Page 4: Visual Basic Lab Manual

4 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Button1 = MessageBox

Step 4. Now, double click on button for code.

Step 5. After the code writing, you can run this program. For running program you must click on start button,

Step 6: During executation

Page 5: Visual Basic Lab Manual

5 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 7: To stop the program now you click on End button,

Program 2: Write a program that add to numbers and display the answer.

Solution

Step 1: Select Form1.

Step2: Add two textboxes (text box1 and text box2).

Step 3: Add three labels First for (Enter first value), second(Enter second value) and third (Answer).

Step 4: Place a button also change the name property into Calculate.

Page 6: Visual Basic Lab Manual

6 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 5: Now double click on button calculate to write code,

Program 3: Write a program in which we input a number and that display it square.

Solution

Step 1: Select form1.

Step 2: Add a label for Enter number for finding the square.

Step 3: Add a text box in which you write any number.

Step 4: Add two buttons by name of Exit and Calculate.

Page 7: Visual Basic Lab Manual

7 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 5: Now double click on the calculate button for code and exit button.

Program 4: Write code for cube according to the program 3.

Solution:

Step 1: Select form1.

Step 2: Add a label for Enter number for finding the cube of give number.

Step 3: Add two text boxes in which you write any number and other text box for display answer.

Step 4: Add two buttons by name of Clear (both text boxes clear) and Calculate.

Page 8: Visual Basic Lab Manual

8 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 5: Now double click on the calculate button for code and clear button.

Program 5: Write a program in which you enter your marks and that display the Percentage and grade.

Solution:

Step 1: Select form1.

Step 2: Add two labels for Enter your marks and Enter total marks for finding the Percentage.

Step 3: Add two text boxes in which you write your marks and total marks.

Step 4: Add a buttons by name Check.

Page 9: Visual Basic Lab Manual

9 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 5: Now double click on the check button for finding percentage.

Lab # 2

Objective: The object is to understand the concept the if-statement.

Theory

THE if STATEMENT: To make a decision in simplest way in Visual Basic program is to use the If…Then statement.

Program 6: Write a program which compares two numbers, which number is greater/ less.

Solution:

Step 1: Select form1.

Step 2: Add two labels for Enter first number and Enter second number for comparing numbers.

Page 10: Visual Basic Lab Manual

10 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 3: Add two text boxes in which you write numbers.

Step 4: Add a buttons by name compare.

Step 5: Now double click on the compare button for the code.

Program 7: Write a program that get user name and password and show the next form.

Solution

Step 1: Select form1 and form2

Step 2: Add two labels for User name and Password in form1.

Step 3: Add two text boxes in which you write name and Password.

Page 11: Visual Basic Lab Manual

11 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 4: Add a buttons by name Sing in.

Form2

Step 5: Add a picture box then select a picture.

Step 6: Add a button Log out that exit the program.

Step 7. Now double click on the Sign in button of form1 for code.

Page 12: Visual Basic Lab Manual

12 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Lab # 3

Objective: The object is to understand the concept of Select case statement.

Theory

SELECT CASE STATEMENT:

It is more flexible and a clearer format.

Program 8: Write a program that get marks from user and give output in Grade/Pass and fail.

Solution:

Step 1: Select form1.

Step 2: Add a labels for Enter your marks for Grade.

Step 3: Add a text box for input the marks.

Step 4: Add a buttons by name Check.

Page 13: Visual Basic Lab Manual

13 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 5: Now double click on the check button for the code.

Lab # 4

Objective: The object is to understand the concept of Loops.

Theory

Loop: It is a program that repeat certain number times the statement.

For Loop: The for loop execute the section of program a fixed number of times.

Program 9: By using loop create a program that displays number from 1 to 10.

Solution

Page 14: Visual Basic Lab Manual

14 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 1: Select form1.

Step 2: Add a buttons by name Click Here

Step 3: Now double click on the button click here.

Program 10: By using For loop create a program that get base number and power from user and displays the power of a number.

Solution:

Step 1: Select form1.

Step 2: Add two labels for Enter base and Power for finding power of a number.

Step 3: Add two text boxes for input of numbers.

Page 15: Visual Basic Lab Manual

15 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 4: Add two buttons one for clear text boxes and next for calculate the power.

Step 5: Now write the code,

Program 11: By using while loop create a program that get a number from user and displays the power of a number.

Solution

Step 1: Select form1.

Step 2: Add a label for Enter number.

Step 3: Add text boxes for input of number.

Step 4: Add a buttons for finding square.

Page 16: Visual Basic Lab Manual

16 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 5: Now write the code,

Program 12: Create a program that displays odd and even numbers.

Solution

Step 1: Select form1.

Step 4: Add two buttons Odd and Even, to print these numbers on form.

Page 17: Visual Basic Lab Manual

17 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 3: Write the code for odd and even number using for loop,

Program 13: Create a table by using loop.

Solution

Step 1: Select form1.

Step 2: Add a label for Enter number for which you want to display the table.

Step 3: Add text boxes for input of number.

Step 4: Add a list box for displaying the result of table.

Step 5: Add a buttons click here.

Page 18: Visual Basic Lab Manual

18 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 6: Now write the code for the table,

Program 14: Create a program to display 10 time hello world! Message by using do-while loop.

Solution

Step 1: Select form1.

Step 2: Add a buttons for display “hello” 10 times by using while loop.

Page 19: Visual Basic Lab Manual

19 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 3: Now write the code,

Program 15: Write a program for bold text and Italic font using check boxes.

Solution

Step 1: Select form1.

Step 2: Add a text box for input some text.

Step 3: Add two check boxes first for font bold and other for font italic.

Page 20: Visual Basic Lab Manual

20 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 4: Now write the code of that program,

Program 16: Write a program that display time, date, month, year and day.

Solution

Step 1: Select form1.

Step 2: Add five labels for (day, time, date, month and year).

Page 21: Visual Basic Lab Manual

21 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 3: Write the code of the program,

Program 17: By using check boxes write a program that display time, date, month, year and day.

Solution

Step 1: Select form1.

Step 2: Add five labels for display (month, year, hour, minutes and seconds).

Step 3: Add five check boxes (month, year, hour, minutes and seconds).

Step 4: Add a button OK.

Page 22: Visual Basic Lab Manual

22 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 5: Write the following code for the program,

Program 18. Write a program that get input (in input box) from user and give answer.

Page 23: Visual Basic Lab Manual

23 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Code

Program 19. By using list box, write a program that get input (name), and perform add, remove, clear and font (Bold, italic and underline).

Page 24: Visual Basic Lab Manual

24 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Code

Page 25: Visual Basic Lab Manual

25 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Program 20: Create a program that show all types dialog boxes, like (OK only, Ok cancel, Yes No, Error, Exclamation, Information, try again etc).

Code

Page 26: Visual Basic Lab Manual

26 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Program 21: By using Array write a program that gets input box user and display these number with their position.

Code

Page 27: Visual Basic Lab Manual

27 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Strings

Program 22. Write a program that gets strings from user and concatenate the strings.

Solution

Step 1: Select form1.

Step 2: Add two text boxes for input some text.

Step 3: Add a button which concatenate both text boxes text.

Step 4: Add two labels for display the concatenated result of text.

Step 5: Now write the code for the program,

Page 28: Visual Basic Lab Manual

28 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Program 23. Write a program in you use like keyword which show the difference like True/False.

Solution

Step 1: Select form1.

Step 2: Add a button for Like Demo.

Step 3: Write the code for the program,

Page 29: Visual Basic Lab Manual

29 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Program 24. Write a program that in that works like a windows explorer, by using drives, folders, files and pictures, also show file’s location.

Solution

Code

Page 30: Visual Basic Lab Manual

30 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Graphics

Program 25. Write a program that show a pixel point on the form.

Code

Page 31: Visual Basic Lab Manual

31 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Program 26. Write a program that print line, that gets input using two text boxes first(get border style from 0 to 6) and second (get border width).

Code

Program 27: Write a program that display graphics shapes (line, circle and rectangle).

Page 32: Visual Basic Lab Manual

32 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Code

Program 28: Write a program that tells us the Location of button.

Solution

Page 33: Visual Basic Lab Manual

33 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Code

Program 29: Write a program that displays the size (Height and width) of form.

Solution

Page 34: Visual Basic Lab Manual

34 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Code

Program 30: write a program that draw a graphical picture of circle with different styles.

Solution

Step 1: Select form1.

Step 2: Add a picture box.

Step 3: Add three text boxes for input of number.

Step 4: Add two buttons for (Draw picture and remove the picture).

Step 5: Add a check box for Auto Draw picture.

Page 35: Visual Basic Lab Manual

35 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic

Step 6: Now write the code for the program,

Page 36: Visual Basic Lab Manual

36 Shah Abdul Latif University Khairpur (mir’s), Sindh Department of Computer Science

Visual Basic