Download as pdf or txt
Download as pdf or txt
You are on page 1of 36

Enrollment No.

______________

Practical-1
Aim: -To study about Visual Basic Editor -----------------------------------------------------------------------------------------------------------Visual Basic is a high level programming language which evolved from the earlier DOS version called BASIC. BASIC means Beginners All Purpose Symbolic Instruction Code. It is very easy programming language to learn.
In BASIC, programming is done in a text-only environment and the program is executed sequentially. In VB, programming is done in a graphical environment.

Visual Basic Editor:-

6 3

Figure-1

Enrollment No.______________

Figure 1 show the Visual Basic Editor Window, which contains (1) Form Window (2) Form Layout Window (3) Project Explorer (4) Properties Window (5) Properties Help Pane (6) Tool Box (7) Tool Bar (8) Code Window

Form Window:-

The Form Window is central to visual basic application developing. It is the place where you design your application in Visual Basic, the place where all the controls are placed.
2

Enrollment No.______________

Form Layout Window:-

From this window we can see the position of the form at the time of execution. Above figure shows this thing.

Project Explorer Window & Properties Window:-

Enrollment No.______________

Project explorer window displays the project name along with all forms that are included in the project. In a project there may be n number of forms according to the project. Properties window is used to establish initial property values for objects. The drop-down box at top of the window lists all objects in the current form. Two views are available: Alphabetic and Categorized. Under this box are the available properties for the currently selected object.

Tool Box:-

Enrollment No.______________

Pointer Picture Box Label Textbox Frame Command Button Check Box Option Button Combo Box List Box H Scroll Bar V Scroll Bar Timer Drive list Box Folder List Box File List Box Shape Line Image Data

Pointer is used to select a control, change size, location of the control or form and so on Picture Box Displays the picture Used to display text on the screen which can not be edited It is used to accept input or display message Frames are used as container to hold other controls like textbox, command button, labels and so on The command Button is a tool which is used to make some action happen Check Box is a box with a tick mark inside it similar to a checkbox, but the only difference is, that one can select more than one item It is a drop down list which contains items It Displays items and one can select more than one item Creates a Horizontal Scroll Bar Creates a Vertical Scroll Bar Creates a timer on the form and if enabled executes the codes regularly after certain interval Used to create a box which contains all disk drives Used to create a box which displays the directory Displays a Group of files Draws the shape selected on the form Draws the line on the form Displays image on the form Represents the database

Tool Bar:-

Enrollment No.______________

Code Window:-

IntelliSense:While writing program/codes, different properties or methods are accessed. It is sometimes not possible to remember the exact syntax for a property or a method. IntelliSense is similar to auto completion systems. There are two types of IntelliSense. 1) Auto List Members 2) Auto Quick Info

Enrollment No.______________

Practical 2
Aim: - To study about following Controls 1) Form 2) Label 3) Text Box 4) Command Button -------------------------------------------------------------------------------------------------------------

1) Form:Form Window:-

The Form Window is central to visual basic application developing. It is the place where you design your application in Visual Basic, the place where all the controls are placed.
7

Enrollment No.______________

Form is the first object we see when we open the application. It is the window into which all the controls will appear where we will input data and see results. There is not too much we can do with form, at this time. Basically we adjust the BackColor and the StartUpPosition and we start putting controls on it. (StartUpPosition indicates that where the form will open on the screen when we run it)

In a single visual basic project there may be more than one forms present as per the requirement of the user.

2) Label:The label tool is the simplest and the most commonly used tool. It is used to display static text, titles and screen output from operations. Labels are used when the text is not to be changed by the user. The prefix for the control should be lbl.
8

Enrollment No.______________

Property Name BackColor Caption Enabled

Value Default (Label1) Colors Any Value True/False

Font Forecolor TabIndex ToolTipText Visible

Colors Any integer value Any value True/False

Description Identifies the object with the name specified Sets the background color of the label Sets the text which will be displayed in the label Determines the label can respond to the user. Enabled=false then user can not perform any action for this label Returns the font object for the label Sets the forecolor of the label to the color specified. Sets the tab order for the label Displays the specified text when the mouse is paused over this label If true then label is visible otherwise not visible

3) Text Box:A textbox is a control in which, the user inputs some values like strings, numbers or special characters. It is normally used when the programmer wants to take the input from the user. Output can also be printed into the textbox. There can be n number of textboxes in a single form. User can move from one textbox to another textbox using mouse or by pressing the Tab key. All operations like copy, cut and paste are possible. The programmer can enable or disable the textbox. The prefix for the control should be txt. Property Name BackColor Enabled Value Default (Text1) Colors True/False Description Identifies the object with the name specified Sets the background color of the textbox Determines whether textbox can respond to the user. If enabled=false then user can not input any value but see the value written in the textbox. Returns the font object for the textbox
9

Font

Enrollment No.______________

ForeColor Index Locked MaxLength Multiline Password Char TabIndex TabStop

Colors Any integer value True/False Any integer value True/False Special characters Any integer value True/False

Text ToolTipText Visible

Any value Any value True/False

Sets the forecolor of the textbox to the color specified Sets the number identifying the textbox in the control array Determines whether the value of the textbox can be edited or not Specifies the maximum number of characters that can be entered in the textbox Determines whether textbox have multiple lines Sets the value of the textbox to the character mentioned Sets the Tab order for the textbox If true than textbox will gain focus when user presses the Tab key otherwise will not gain Focus Returns or sets the specified value in the textbox Displays the specified text when the mouse is paused over the textbox If true textbox is visible on the form otherwise invisible

4) Command Button:The command button is used to initiate actions, usually by clicking on it. This is used in all applications. The user interacts with the program by clicking buttons. The prefix for control should be cmd. Property Name BackColor Cancel Value Default (Command1) Colors True/False Description Identifies the object with name specified Sets the background color of the command button Indicates whether a command button is the cancel button on the form. If set true then cancel button is accessed by Ecs Key
10

Enrollment No.______________

Caption Default

Any value True/False

Enabled

True/False

Font Index TabIndex TabStop

Any integer value Any integer value True/False

ToolTipText Visible

Any value True/False

Sets the text which will be displayed in the command button Determines whether the command button is the default button on the form. If set true then it is accessed by pressing Enter key Determines whether the command button can respond to the user. If enable=false then user can not perform any action Returns the font object for the command button Sets the number identifying the command button in the control array Sets the Tab order for the command button If it is true then command button will gain focus when Tab key is pressed otherwise will not gain focus Displays the specified text when the mouse is paused over this command button If true then command button is visible on the form otherwise invisible

11

Enrollment No.______________

Practical-3
Aim:-Write a program that display a message Hello World in Label ------------------------------------------------------------------------------------------------------------ The user interface is shown below in the figure

Object Form Frame Label Command Button Name Caption Name Caption Name Caption Name Caption

Properties

Set Value Form6 Display Message Frame1 Practical 3 Label1 Command1 Click Here

12

Enrollment No.______________

The Code Is given below Private Sub Form_Load() End Sub Private Sub Frame1_DragDrop(Source As Control, X As Single, Y As Single) End Sub Private Sub Label1_Click() End Sub Private Sub Command1_Click() Label1.Caption = Hello World End Sub

The final output is shown below in the figure

13

Enrollment No.______________

Practical-4
Aim:- Write a Program that input two integer numbers within textbox ------------------------------------------------------------------------------------------------------------ User interface is shown below in the figure

Objects Form Frame Label Textbox

Property Name Caption Name Caption Name Caption Name MaxLength

Set Value From7 Input Two Numbers Frame1 Practical 4 Label1 Enter Numbers Here Text1 2
14

Enrollment No.______________

The code is shown below Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case vbKey0 To vbKey9 Case vbKeyBack, vbKeyClear, vbKeyDelete Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab Case Else KeyAscii = 0 Beep End Select End Sub Final Output is shown below in figure

Here user can not input any character in the textbox only numeric entries allowed.
15

Enrollment No.______________

Practical-5
Aim:-write a program that read 3 numbers using one text box. Whenever user clicks on the max button, display the maximum number in the label ------------------------------------------------------------------------------------------------------- The user interface is shown below in the figure

Object Form Frame Textbox Textbox Textbox Command Button Label Name Caption Name Caption Name Name Name Name Caption Name

Property

Set Value Form8 Form8 Frame1 Frame1 Text1 Text2 Text3 Command1 MAX Label1

16

Enrollment No.______________

The code is shown below Private Sub Text1_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case vbKey0 To vbKey9 Case vbKeyBack, vbKeyClear, vbKeyDelete Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab Case Else KeyAscii = 0 Beep End Select End Sub Private Sub Text2_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case vbKey0 To vbKey9 Case vbKeyBack, vbKeyClear, vbKeyDelete Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab Case Else KeyAscii = 0 Beep End Select End Sub

Private Sub Text3_KeyPress(KeyAscii As Integer) Select Case KeyAscii Case vbKey0 To vbKey9 Case vbKeyBack, vbKeyClear, vbKeyDelete Case vbKeyLeft, vbKeyRight, vbKeyUp, vbKeyDown, vbKeyTab Case Else
17

Enrollment No.______________

KeyAscii = 0 Beep End Select End Sub Private Sub Command1_Click() Dim a, b, c As Double a = Text1.Text b = Text2.Text c = Text3.Text If (a > b And a > c) Then Label1.Caption = a End If If (b > c And b > a) Then Label1.Caption = b End If If (c > a And c > b) Then Label1.Caption = c End If End Sub

18

Enrollment No.______________

Final Output is shown below in figure

19

Enrollment No.______________

Practical-6
Aim:-Write a visual basic program that read 10 numbers using textbox. Display those numbers in the label. Create one command button SORT. Whenever user click on this command button, numbers are arrange in ascending order ------------------------------------------------------------------------------------------------------ The user interface is shown below in the figure

20

Enrollment No.______________

Practical-7
Aim:-Develop a Calculator that performs addition, subtraction, multiplication and division operations ---------------------------------------------------------------------------------------------------------- The user interface is shown below in the figure

Object Textbox Command Button Command Button Command Button Command Button Name Name Caption Name Caption Name Caption Name

Property

Set Value Text1 Command1 7 Command2 8 Command3 9 Command4


21

Enrollment No.______________

Command Button Command Button Command Button Command Button Command Button Command Button Command Button Command Button Command Button Command Button Command Button Command Button Command Button Command Button

Caption Name Caption Name Caption Name Caption Name Caption Name Caption Name Caption Name Caption Name Caption Name Caption Name Caption Name Caption Name Caption Name Caption Name Caption

4 Command5 5 Command6 6 Command7 1 Command8 2 Command9 3 Command13 CE Command14 0 Command15 . Equals = signb +/Sub Div / Mul X Add +

The code is shown below Dim firstno As Single Dim secondno As Single Dim ansno As Single Dim mbutton As Integer
22

Enrollment No.______________

Dim sign As Boolean

Private Sub Add_Click() mbutton = 1 firstno = Val(Text1) Text1 = "" Command15.Enabled = True End Sub Private Sub Command1_Click() Text1.Text = Text1.Text & 7 End Sub Private Sub Command10_Click() Form3.Show Form1.Hide Form2.Hide End Sub Private Sub Command13_Click() Text1.Text = "" Command15.Enabled = True End Sub Private Sub Command14_Click() Text1.Text = Text1.Text & 0 End Sub Private Sub Command15_Click() Text1.Text = Text1.Text & "." Command15.Enabled = False End Sub

Private Sub Command2_Click()


23

Enrollment No.______________

Text1.Text = Text1.Text & 8 End Sub Private Sub Command3_Click() Text1.Text = Text1.Text & 9 End Sub Private Sub Command4_Click() Text1.Text = Text1.Text & 4 End Sub Private Sub Command5_Click() Text1.Text = Text1.Text & 5 End Sub Private Sub Command6_Click() Text1.Text = Text1.Text & 6 End Sub Private Sub Command7_Click() Text1.Text = Text1.Text & 1 End Sub Private Sub Command8_Click() Text1.Text = Text1.Text & 2 End Sub Private Sub Command9_Click() Text1.Text = Text1.Text & 3 End Sub Private Sub Div_Click() mbutton = 4 firstno = Val(Text1) Text1 = ""
24

Enrollment No.______________

Command15.Enabled = True End Sub Private Sub Equals_Click() secondno = Val(Text1) Select Case mbutton Case Is = 1 ans = firstno + secondno Case Is = 2 ans = firstno - secondno Case Is = 3 ans = firstno * secondno Case Is = 4 ans = firstno / secondno End Select Text1 = ans End Sub Private Sub Mul_Click() mbutton = 3 firstno = Val(Text1) Text1 = "" Command15.Enabled = True End Sub Private Sub signb_Click() If Text1 = "-" & Text1 Then MsgBox "error" End If
25

Enrollment No.______________

If signb = False Then Text1 = "-" & Text1 signb = True Else minusvalue = Val(Text1) minusvalue = (-1 * Text1) Text1 = minusvalue signb = False End If End Sub Private Sub Sub_Click() mbutton = 2 firstno = Val(Text1) Text1 = "" Command15.Enabled = True End Sub Final Output is shown below in figure Here we done multiplication function 14 X 180 = 2520

26

Enrollment No.______________

27

Enrollment No.______________

Practical-8
Aim: - To study about following controls 1) List Box 2) Combo Box 3) Check Box 4) Option Button 5) Frame -------------------------------------------------------------------------------------------------------------

1) List Box:A list box is the control in Visual basic that contains a list of items. With the list box control user can select items from a list of choices. Although the list of choices can be entered in the list property of the control, as in the example below, this is not a good habit to get into. There are different types of list boxes. The different types can be determined by the style and multiselect property. The different styles are as follows. 0-Standard 1-Checkbox The default style is o-Standard, which indicates the no checkbox is placed next to the item. Multiple items can be selected in the list box if the multiselect property is set to simple or extended. The style 1-Checkbox indicates that a checkbox is placed next to the item. Multiple items can be selected by clicking the checkbox. The common technique for loading a ListBox is to do it in code, with the Form_Load event. This way, if items have to be added it is a simple matter to add a line of code.

28

Enrollment No.______________

2) Combo Box:The combo box is called because it is a combination of list box and text box. It has the advantage over the list box of not taking up space until it is actually used which means that it makes it easier to position an the form. But the combo box has the disadvantage, sort of that the user can enter his own information, in addition to what is the list. This may take data validation

29

Enrollment No.______________

harder because the choices are not limited. When we want to force the user to choose among the specified items we can use the combo box. As in the list box, use the text property to get the information input. Label3.Caption = cbo_position.Text

It is fairly simple to load the ListBox and the ComboBox during the From_Load event. The only other detail to note is that the order in which the items appear in the Combo is not the same as the order in which the items were added. That is intentional - it is done with the Sorted property for the ComboBox. It can also be done for the ListBox.

3) Check Box:A checkbox is a box with a tick inside it. The tick mark represents that the checkbox is selected. A checkbox is normally used to select multiple itemsfor the list.

30

Enrollment No.______________

The value of checkbox is 0 if it is unchecked, 1 if it is selected or checked. The prefix for the control should be chk. The list of all the properties along with description is displayed below. Property Name Back Color Caption Enabled Font Forecolor Index TabIndex TabStop Value Default (Check1) Colors Any value Description Identifies the object with the name specified

ToolTipText Value

Visible

Sets the background color of the checkbox Sets the text which will displayed in the checkbox True/False Determines whether the checkbox can respond to the user Returns the font object for the checkbox Colors Sets the forecolor of the checkbox to the color specified Any integer Sets the number identifying the checkbox in value the control array Any integer Sets the Tab order for the checkbox value True/False If true then checkbox will gain focus when the user presses the Tab key otherwise will not gain focus Any value Displays the specified text when the mouse is paused over this checkbox 0-Unchecked The checkbox is not selected 1-Checked The checkbox is selected 2-Greyed The checkbox is disabled True/false The checkbox is visible if the value is true otherwise invisible.

4) Option Button;The option button is similar to checkbox, but the only difference is that one can select only one item from the list. Option button placed on the form function as group.

31

Enrollment No.______________

For adding option button do not double click on the tool as it will add the tool on the form and not on the frame. Instead click on the tool go inside the frame and click and drag. Now the tool will be placed on the frame. The prefix for control should be opt. Property Name Back Color Caption Enabled Font Forecolor Index Style Value Default (Option1) Colors Description Identifies the object with the name specified

TabIndex TabStop

ToolTipText Value Visible

Sets the background color of the option button Any value Sets the text which will displayed in the option button True/False Determines whether the option button can respond to the user Returns the font object for the option Button Colors Sets the forecolor of the option button to the color specified Any integer Sets the number identifying the option value button in the control array 0-Standard Sets the appearance of the option button to 1-Graphical standard window style Sets the appearance of the option button to graphical with custom picture or color Any integer Sets the Tab order for the option button value True/False If true then option button will gain focus when the user presses the Tab key otherwise will not gain focus Any value Displays the specified text when the mouse is paused over this option button True/False If true the option button is selected otherwise not selected True/false The option button is visible if the value is true otherwise invisible.

32

Enrollment No.______________

5) Frame:Frames are used as a container to hold other controls like textbox, command button, labels etc. a frame can contain another frame too. Any control can be added on the frame. Frames are normally used to group checkboxes or option buttons. The prefix for the control should be fra. Property Name Back Color Caption Enabled Font Index TabIndex ToolTipText Visible Value Default (Frame1) Colors Any value Description Identifies the object with the name specified

Sets the background color of the Frame Sets the text which will displayed in the Frame True/False Determines whether the Frame can respond to the user Returns the font object for the Frame Any integer Sets the number identifying the Frame in the value control array Any integer Sets the Tab order for the Frame value Any value Displays the specified text when the mouse is paused over this Frame True/false The Frame is visible if the value is true otherwise invisible.

33

Enrollment No.______________

Practical-9
Aim:- Write a program that reads temperature in Celsius or in Fahrenheit based on value of the option buttons to Celsius and to Fahrenheit. Convert the temperature in either Celsius or in Fahrenheit ------------------------------------------------------------------------------------------------------------ The user interface is shown below in the figure

Object Form Textbox Textbox Option Button Option Button Name Caption Name Name Name Caption Name Caption

Property

Set Value Form10 Form10 Text1 Text2 Option1 To Celsius Option2 To Fahrenheit
34

Enrollment No.______________

The code is shown below

35

Enrollment No.______________

Final Output is shown below in figure

36

You might also like