Visual Basic Programming1
Visual Basic Programming1
Unit 1
Part A (2 Marks)
1. Write any three common properties for several visual basic controls.
BackColor and ForeColor- The BackColor property sets the background color of an object while the
ForeColor property changes the foreground color used to display text.
Caption-It sets the text displayed in the object's title bar or on the object.
Font-You can set the font property from the Properties Window.
2. What is toolbox? List the common collection of tools.
Displays the standard Visual Basic controls plus any ActiveX controls and inser table objects you have
added to your project. The tools in toolbox are label, textbox, combobox, option button, checkbox,
command button.
3. Define Variables. How to declare variable
variables are areas allocated by the computer memory to hold data. In Visual Basic, it is a good
practice to declare the variables before using them by assigning names and data types. They are
normally declared in the general section of the codes' windows using the Dim statement.
Dim VariableName As DataType
Dim yourName As String
Dim firstnum As Integer
4. Define Constants.
Constants are different from variables in the sense that their values do not change during the running of
the program.
Constant Name As Data Type = Value
Const Pi As Single=3.142
5. List the built_in functions with example.
Rnd is is very useful function for dealing with the concept of chance and probability.
Print Rnd
Int is the function that converts a number into an integer by truncating its decimal part.
Int(0.032)=0
Log is the function that returns the natural Logarithm of a number. For example,
Log 10= 2.302585
6. How to declare multidimensional arrays in VB?
Dim a(2,3) as integer
2 represent rows
3 represents column
7. Define array.
An array is a set of values that are logically related to each other, such as the number of students in each
grade in a grammar school.
Declare a single-dimension array of 5 values
Dim numbers(4) As Integer
8. Write the syntax of inputbox function. Explain.
An InputBox( ) function will display a message box where the user can enter a value or a message in the
form of text. The format is
myMessage=InputBox(Prompt, Title, default_text, x-position, y-position)
1
Visual Basic Programming
Unit 1
Part B (5 Marks)
Gets or sets a value indicating whether the control redraws itself when resized.
ResizeRedraw
2
Visual Basic Programming
3.
Write the properties of textbox control.
AutoSize Gets or sets a value indicating whether the height of the control automatically adjusts when
the font assigned to the control is changed.
Font Gets or sets the font of the text displayed by the control.
Maxlength Gets or sets the maximum number of characters the user can type or paste into the text box
control.
Multiline Gets or sets a value indicating whether this is a multiline TextBox control.
Form is the work place in visual basic were the user can place controls in it the header area as a
caption. Control menu and maximums , minima’s closed button.
The large area of the form is called the client area in visual basic the basic building block of on
application is a form which is simple a window and controls like check box, text box, label etc..or
place in it….
Properties :-
Captions :- this properties set the title of the form by default the caption is form one.
Name:- the user can change the name of the form as per his wish by default form1. Is the name this
properties is used while writing codes
Appearance :- this properties desides the forms appearance like normal 2d,3d
-> none
1-> fixed single
2-> size able
3-> fixed double
4-> fixed tool window
5-> sizeable tool window.
Font :- the user can set the font, font style and size by using this properties
4
Visual Basic Programming
Height width :- this properties desides the height and width of a form.
Window state :- specified the state of the window like maximize minimize and normal
Events :-
Load :- this event is fixed when the form is loaded secondary memory to the primary memory
Unload :- this event is fired when the user closed the form.
Got focus :- fired when the controls received the input focus
Los focus :- fires when the control leaves the passes to another control
Key down :- respond when the user translate the key ansi numeric code
Unit 1
5
Visual Basic Programming
Tool box contains a set of controls with are used to customized form using this controls and interface between
user and application can be created by default tool box contains 21 activex controls to place a control on a form
select it with the mouse and then move the mouse over the form. And draw the control on the form. Otherway is
just double click on the required controls.
Text box:- text box is used to accept user input and it can also display some information.
Label :- label is used to display text in a form which cannot be edited during run time
Command button :- command button used to initate and action by pressing on the button
Check box :- check box used to do a choice for user multiple choices can be made.
Option button:- option button used in groups where one at a time can be true
Combo box :- used to provided a list of items from which user can select any one.
List box :- used to provided a list of items from which user can select multiple options
Image :- used to display images but less capacity than the picture box.
Ole :- [object link embaded] used to interact with other windows application.
6
Visual Basic Programming
Consist Of A Number Of Element Including Menu Bar, Title Bar, Tool Bar, Tool Box, Project Explorer And
Properties Window. The Main Window In The Middle Of the Screen Contain A Form Named Form One The
Form Is The Application Window Were The Uses Interfaces Can Be Placed
Menu Bar :- The Menu Bar Contains The Command Needed To Works With Visual Basic
Project explorer
7
Visual Basic Programming
Project explorer displaced the components of the project. The user can switch over form code to design view
and viz..
Properties windows
Properties windows displays the properties of a form on a control.properties Are artibutes of an object
search as its size,name,captions and color.
Form layout
The form layout window in the lower right corner of the vb IDE. It is use to determain the initial
position form in the application this window is usefull for the application that use multipul forms
Tool box :-
Tool box contains a set of controls with are used to customized form using this controls and interface
between user and application can be created by default tool box contains 21 activex controls to place a control
on a form select it with the mouse and then move the mouse over the form. And draw the control on the form.
Otherway is just double click on the required controls.
8
Visual Basic Programming
Text box:- text box is used to accept user input and it can also display some information.
Label :- label is used to display text in a form which cannot be edited during run time
Command button :- command button used to initate and action by pressing on the button
Check box :- check box used to do a choice for user multiple choices can be made.
Option button:- option button used in groups where one at a time can be true
Combo box :- used to provided a list of items from which user can select any one.
List box :- used to provided a list of items from which user can select multiple options
Image :- used to display images but less capacity than the picture box.
Ole :- [object link embaded] used to interact with other windows application.
Form :-
9
Visual Basic Programming
Form is the work places in visual basic were the user can place controls in it the header area as a caption.
Control menu and maximums, minima’s closed button.
The large area of the form is called the client area in visual basic the basic building block of on
application is a form which is simple a window and controls like check box, text box, label etc..or place in it.
10
Visual Basic Programming
Unit II
Part A (2 Marks)
5. Define array.
An array is a set of values that are logically related to each other, such as the number of students in each
grade in a grammar school.
Declare a single-dimension array of 5 values
Dim numbers(4) As Integer
11
Visual Basic Programming
Unit II
Part B (5 Marks)
Case value2
Block of one or more VB Statements
Case Else
Block of one or more VB Statements
End Select
Example :
Dim grade As String
Private Sub Compute_Click( )
grade=txtgrade.Text
Select Case grade
Case "A"
result.Caption="High Distinction"
Case "A-"
result.Caption="Distinction"
Case "B"
result.Caption="Credit"
Case "C"
result.Caption="Pass"
12
Visual Basic Programming
Case Else
result.Caption="Fail"
End Select
End Sub
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
Example:
Do while
counter <=1000
num.Text=counter
counter =counter+1
Loop
For.... Next Loop
The For ... Next Loop event procedure is written as follows:
13
Visual Basic Programming
Example:
Private Sub Test_Click()
Dim testmsg As Integer
testmsg = MsgBox("Click to test", 1, "Test message")
If testmsg = 1 Then
Display.Caption = "Testing Successful"
Else
Display.Caption = "Testing fail"
End If
End Sub
14
Visual Basic Programming
^ Exponential 2^4=16
* Multiplication 4*3=12,
/ Division 12/4=3
Mod Modulus (returns the remainder from an integer division) 15 Mod 4=3
Operator Meaning
= Equal to
15
Visual Basic Programming
Logical Operators
In addition to conditional operators, there are a few logical operators that offer added power to the VB
programs.
Operator Meaning
16
Visual Basic Programming
Unit II
17
Visual Basic Programming
You can define a Function procedure in a module, class, or structure. It is Public by default, which means you
can call it from anywhere in your application that has access to the module, class, or structure in which you
defined it.
A Function procedure can take arguments, such as constants, variables, or expressions, which are passed to it by
the calling code.
Syntax:
Public Function functionName (Arg As dataType,. .........) As dataType
or
Private Function functionName (Arg As dataType,. ......... ) As dataType
* Public indicates that the function is applicable to the whole project
Public Function grade(mark As Variant) As String
Select Case mark
Case Is >= 80
grade ="A"
Case Is>= 70
grade ="B"
Case Is >= 60
grade ="C"
Case Is >= 50
grade ="D"
Case Is >= 40
grade ="E"
Case Else
grade ="F"
End Select
End Function
Private Sub compute_Click()
grading.Caption = grade(mark)
End Sub
18
Visual Basic Programming
x = Val(TxtNum1.Text)
y = Val(TxtNum2.Text)
sum x, y
End Sub
19
Visual Basic Programming
UNIT 3
PART A(2 MARKS)
1. What is meant by object?explain
An object is a combination of code and data that can be treated as a unit. An object can be a piece of an
application, like a control or a form. An entire application can also be an object.
UNIT 3
PART C (10 MARKS)
1. What is control array? How to create it? Explain
A control array is a group of controls that share the same name type and the same event procedures. Adding
controls with control arrays uses fewer resources than adding multiple control of same type at design time.
A control array can be created only at design time, and at the very minimum at least one control must belong to
it. You create a control array following one of these three methods:
You create a control and then assign a numeric, non-negative value to its Index property; you have thus
created a control array with just one element.
You create two controls of the same class and assign them an identical Name property. Visual Basic
shows a dialog box warning you that there's already a control with that name and asks whether you want to
create a control array. Click on the Yes button.
You select a control on the form, press Ctrl+C to copy it to the clipboard, and then press Ctrl+V to paste
a new instance of the control, which has the same Name property as the original one. Visual Basic shows the
warning mentioned in the previous bullet.
Control arrays are one of the most interesting features of the Visual Basic environment, and they add a lot of
flexibility to your programs:
Controls that belong to the same control array share the same set of event procedures; this often
dramatically reduces the amount of code you have to write to respond to a user's actions.
You can dynamically add new elements to a control array at run time; in other words, you can
effectively create new controls that didn't exist at design time.
The fact that multiple controls can share the same set of event procedures is often in itself a good reason
to create a control array. For example, say that you want to change the background color of each of your
TextBox controls to yellow when it receives the input focus and restore its background color to white
when the user clicks on another field:
Private Sub Text1_GotFocus(Index As Integer)
Text1(Index).BackColor = vbYellow
End Sub
Private Sub Text1_LostFocus(Index As Integer)
20
Visual Basic Programming
Text1(Index).BackColor = vbWhite
End Sub
The following code is entered in the cmd_Click( ) (Control Array) event procedure
The following code is entered in the click events of the cmdPlus, cmdMinus, cmdMultiply, cmdDevide controls
respectively.
21
Visual Basic Programming
To print the result on the text box, the following code is entered in the cmdEqual_Click ( ) event procedure.
Case "+"
Result = Previous + Current
txtDisplay.Text = Result
Case "-"
Result = Previous - Current
txtDisplay.Text = Result
Case "*"
Result = Previous * Current
txtDisplay.Text = Result
Case "/"
Result = Previous / Current
txtDisplay.Text = Result
End Select
22
Visual Basic Programming
Current = Result
End Sub
Creating Menus
Open a new Project and save the form as menu.frm and save the project as menu.vbp.
Choose Tools ››› Menu Editor and type the menu items as shown below.
Caption Name
File mnuFile
Open mnuOpen
Save mnuSave
Exit mnuExit
Edit mnuEdit
Copy mnuCopy
Cut mnuCut
Paste mnuPaste
23
Visual Basic Programming
24
Visual Basic Programming
UNIT IV
PART A (2 MARKS)
5. Write the steps for adding the common dialog box control to the toolbox window.
To add the common dialog box to the tool box
Choose project menucomponentsmicrosoft common dialog control and click ok. The control will be
added to the toolbox.
25
Visual Basic Programming
UNIT IV
PART B (5 MARKS)
26
Visual Basic Programming
Visual Basic responds to various mouse events, which are recognized by most of the controls. The main
events are MouseDown, MouseUp and MouseMove.
MouseDown occurs when the user presses any mouse button.
MouseUp occurs when the user releases any mouse button.
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
These events use the arguments button, Shift, X, Y and they contain information about the mouse's condition
when the button is clicked.
The first argument is an integer called Button. The value of the argument indicates whether the left, right or
middle mouse button was clicked.
The second argument in an integer called shift. The value of this argumnet indicates whether the mouse
button was clicked simultaneously with the Shift key, Ctrl key or Alt key.
The third and fourth arguments X and Y are the coordinates of the mouse location at the time the mouse
button was clicked.
Example :
Private Sub Form_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
If optCredit = True Then
imgCredit.Move X, Y
Else
imgCash.Move X, Y
End If
End Sub
27
Visual Basic Programming
UNIT IV
PART C (10 MARKS)
The common dialog control provides an interface between Visual Basic and the procedures in the Microsoft
Windows dynamic-link library Commdlg.dll.
The common dialog control allows you to display these commonly used dialog boxes:
Open
Save As
28
Visual Basic Programming
Color
Font
Print
To add the common dialog control to the toolbox by selecting Components from the Projectmenu. Locate and
select the control in the Controls tabbed dialog, then click the OK button.
Method Dialog
displayed
ShowOpen Open
ShowSave Save As
ShowColor Color
ShowFont Font
ShowPrinter Print
ShowHelp Invokes
Windows
Help
Displaying Open and Save As Dialog Boxes
The Open dialog box allows the user to specify a drive, a directory, a file name extension, and a file name.
The Save As dialog box is identical to the Open dialog in appearance, except for the dialog's caption, and file
names appearing dimmed out. At run time, when the user chooses a file and closes the dialog box, the FileName
property is used to get the selected file name.
29
Visual Basic Programming
Color The selected color. To use this property, you must first
set the Flags property to cdlCFEffects.
30
Visual Basic Programming
31
Visual Basic Programming
Right click on Image control a popup menu is open choose properties option.A Properties Page is open .Click
on Images Tab and insert the image as you want. If you want insert more than one picture then click on Insert
Picture Button as shown in figure below:
Right click on the ListView1 Apopup menu is open choose properties option. A Properties page is open. Click
on the imagelist tab and insert the imagelist1 then “OK” as shown in figure below:
32
Visual Basic Programming
Slider Control
A Slider control consists of a scale,defined by the minimum and maximum properties and a Arrow type
button,which can be used to manipulate using the mouse or arrow keys.Slider Control is the part of Microsoft
Common Controls, With the Slider control you have any way to stop the value being printed as you move the
slider by dragging.
Choose the “Slider” control from the list of controls in the toolbox. Place the control on the form in Visual
Basic. Also place one Label1 and Text1 control on the form1.
33
Visual Basic Programming
ProgressBar control
A progressbar control allows you to graphically represent the progress of a transaction. It is used to inform the
user about processing. It shows the user the status of computations/processing.Number of applications, such as
setups, database-driven applications, and file transfer tools, swear by progress bars. Choose
the “ProgressBar” control from the list of controls in the toolbox. Place the control on the form in Visual
Basic.Also add one Label1 and Timer1 control on the Form1.
34
Visual Basic Programming
ToolBar control
A ToolBar control consisit of a collection of button objects used to create a toolbar that can be associated with
an application.It has become one of the most important tools for providing an easy interface to the users.the
toolbar control provides easy access to options available in your applications.A toolbar control contains
acollection of buttons objects used to create a Toolbar. Right click on toolbar a popup menu is open choose
properties from popup menu.Set the properties as desired for the control.You can also add some buttons on the
toolbar.For this you click on the Buttons tab and then click on the Insert Button. you add buttons as you wish.
35
Visual Basic Programming
UNIT V
PART A (2 MARKS)
1. What is called file copy function?
Filecopy function copy a file from source path to destination path
Filecopy <source> <destination>
2. What is OLE?
Object Link Embedded allows an editing application to export part of a document to another
editing application and then import it with additional content.
3. Write drag and drop operation in VB.
Drag-and-drop editing can be accomplished by two different methods: standard dragging, for dragging
between controls on a form, and OLE dragging, for dragging between forms and applications.
PART B (5 MARKS)
1. Explain any six methods of file system object in visual basic.
The File System Object (FSO) object model provides an object-based tool for working with folders and files.
Using "object.method" syntax, it exposes a comprehensive set of properties and methods to perform file
system operations such as creating, moving, deleting, and providing information about folders and files. To
use the FSO with your VB project, you must add a reference to "Microsoft Scripting Runtime" .
Methods of the FileSystemObject
Method Description
BuildPath Appends file path information to an existing file path.
CopyFile Copies files from one location to another.
CopyFolder Copies folders and their contents from one location to another.
CreateFolder Creates a folder.
CreateTextFile Creates a text file and returns a TextStream object.
DeleteFile Deletes a file.
DeleteFolder Deletes a folder and all of its contents.
DriveExists Determines if a drive exists.
FileExists Determines if a file exists.
FolderExists Determines if a folder exists.
GetAbsolutePathName Returns the full path to a file or folder.
GetBaseName Returns the base name of a file or folder.
GetDrive Returns a drive object.
GetDriveName Returns a drive name.
GetExtensionName Returns a file extension from a path.
GetFile Returns a file object.
GetFileName Returns a filename from a path.
GetFolder Returns a folder object.
GetParentFolderName Returns the parent folder name from a path.
GetSpecialFolder Returns an object pointer to a special folder.
GetTempName Returns a temporary (randomly generated) file or folder name that can be used
with CreateTextFile.
MoveFile Moves files from one location to another.
MoveFolder Moves folders and their contents from one location to another.
OpenTextFile Opens an existing text file and returns a TextStream object.
36
Visual Basic Programming
37
Visual Basic Programming
GetDirectories Returns a String collection representing the path names of subdirectories within a
directory.
GetFiles Returns a read-only String collection representing the names of files within a
directory.
SpecialDirectories Gets an object that provides properties for accessing commonly referenced
directories.
38
Visual Basic Programming
Nearly all Visual Basic controls support OLE drag-and-drop to some degree. In addition, some standard and
ActiveX controls provide automatic support for OLE drag-and-drop, which means that the control supports
automatic settings in both their OLEDragMode and OLEDropMode properties, and that no code needs to be
written to either drag from or drop to the control.
Using the following OLE drag-and-drop properties, events, and method, you can specify how a given control
responds to dragging and dropping.
Category Item Description
39