unit 3 vb
unit 3 vb
File handling
The following are three important steps in handling a file.
• Processing the file, i.e. either reading the content of the file or
writing the required data into file or both.
Option Meaning
Pathname Name of the file to be opened.
Value , values: Shared, Lock Read, Lock Write, and Lock Read
Write. Filenumber A number in the range 1 to 511. This number
must be unique among open files. Use FreeFile function to
obtain the next available number. RecLength Specifies the size
of each record in random files. It should be <= 32767.
Examples:
To open TEST.TXT file in input mode:
Open “TEST.TXT” for input as #1
To open NUMBER.DAT file in Binary mode:
Open “NUMBER.DAT” for binary access write as #1
To open STUDENTS.DAT in random mode with a record length
of 10:
Open “STUDENTS.DAT” for random as #1 len = 10
To get next available file number and then use it: 'FreeFile
function returns the number that can be used as the file
'number while opening the file Fn = FreeFile Open
“TEST.TXT” for input as #fn Functions related to files The
following are the functions that are used with files. Function
Meaning Dir Returns the name of the file that matches the
given name. If file is not existing, then it returns "" (null
string).
FileLen Returns the length of the file in bytes. LOF Returns
the length of an open file in bytes. EOF Returns true, if the
specified file has reached end-of-file marker. FreeFile Returns
the next available file number. Seek Sets or returns the
position at which file pointer is currently positioned. For
random files it returns the number of records read or written
so far.
Filecopy Copies the given source file to target file.
GetAttr Returns the attributes of the given path.
SetAttr Changes the attributes of the specified file to the given
attributes. FileDateTime Returns the date and time when file was
last modified or created.
Loc Returns the current position of file pointer of an open file.
Functions related to file
handling. Example:
To find out the length of file CHARS.TXT:
If dir(“students.dat”) = “”
then MsgBox “File
students.dat is missing” Else
Process
the file
End if
READING FILE
Input( ) Returns the specified number of characters from
the given file. Input # Reads data into specified list of variables
from the given file. Line Input # Reads a complete line from the
given file.
Open Opens the given file in the specified mode.
Print # Prints the specified data to the given file.
Put Writes a record to the given position of the specified file.
Write # Writes the specified data to the given file.
Return Value
Remarks
Use the BOF and EOF properties to
determine whether a Recordset object contains records or
whether you have gone beyond the limits of a Recordset object
when you move from record to record.
The BOF property returns True (-1) if the current record position
is before the first record and False(0) if the current record
position is on or after the first record.
The following table shows what happens to the BOF and EOF
property settings when you call various Move methods but are unable
to successfully locate a record.
BOF EOF
All graphics methods described here will use the default coordinate
system
GRAPHIC PROPERTIES
The graphic properties are useful while working with the graphic
methods. Some of the form's properties and some of the PictureBox's
properties are the graphics properties.
The common graphic properties are discussed in this section. You’ll learn
more about them using code examples later in this tutorial.
DrawMode: The DrawMode property sets the mode of drawing for the
appearance of output from the graphic methods. In the DrawMode
property, you can choose from a variety of values.
DrawStyle: The DrawStyle property sets the line style of any drawing
from any graphic methods. It allows you to draw shapes of different
line styles such as solid, dotted, dashed shapes etc.
DrawWidth: The DrawWidth property sets the line width of any
drawing from any graphic methods. While drawing shapes, you can
control the thickness of the lines using this property.
FillColor: The FillColor property is used to fill any shapes with a
color. You may use the symbolic color constants to fill your shapes.
You may also use the color codes as well as the RGB function.
FillStyle: The FillStyle property lets you fill shapes in a particular
filling style.
ForeColor: The ForeColor property is used to set or return the
foreground color.
AutoRedraw: Set the AutoRedraw property to True to get a persistent
graphics when you’re calling the graphic methods from any event, but
not from the Paint event.
ClipControls: Set the ClipControls property to True to make the
graphic methods repaint an object.
Picture: The Picture property is used to set a picture. Pictures can be
set both at design time and run-time.
Drawing points
This section shows you how to draw points using the PSet method and
how to use the Step keyword with the PSet method.
Private Sub
cmdShow_Click()
DrawWidth = 10
CurrentX = 500
CurrentY = 500
PSet Step(0,
0) End Sub
The above code draws a point in the (0, 0) position relative to the current
position that is (500, 500).
That means, the point is drawn in the (500, 500) position. But this is (0, 0)
position relative to the current position.
Drawing lines
The Line method lets you draw lines in Visual Basic 6. You need to
specify the starting point and the finishing point of the line in the argument.
You may also specify the color of the line. This is optional, though.
A simple line
The following code example shows how to draw a simple line using
the Line method in Visual Basic 6.
Example:
Code:
Private Sub cmdShow_Click()
DrawWidth = 5
'A hyphen is required between the points
Line (0, 0)-(2000, 2000), vbBlue
End Sub
Output of code example:
\
A line with drawing styles
Form’s DrawStyle property lets you draw lines using a particular style.
The constant values of the DrawStyle property are 0 (vbSolid), 1 (vbDash), 2
(vbDot), 3 (vbDashDot, 4 (vbDashDotDot), 5 (vbTransparent) and 6
(vbInsideSolid). The default value is 0, vbSolid. You may use the numeric
constant or the symbolic constant such as vbSolid, vbDash etc to change
drawing styles in your code.
NOTE: The DrawStyle property does not work if the value of DrawWidth is
other than 1.
Example:
Code:
DrawWidth = 1
DrawStyle = 1
'A hyphen is required between the points
Line (0, 0)-(2000, 2000), vbBlue
DrawStyle = vbDashDot
Line (100, 900)-(2800, 2800), vbRed
Output:
Drawing circles
You can draw a circle using the Circle method in Visual Basic 6. You
may also use the Circle method to draw different geometric shapes such as
ellipses, arcs etc. You need to specify the circle’s center and radius values to
draw a circle using the Circle method.
A simple circle
The following code draws a simple circle using the Circle method in
Visual Basic 6.
Example:
Code:
Private Sub
cmdShow_Click()
DrawWidth = 3
Circle (1800, 1800), 1000,
vbRed End Sub
In the above code, (1800, 1800) is the circle’s center, and the radius value is
1000. The color constant ‘vbRed’ is an optional argument.
Output of code example 10:
Output:
Rectangle
The Line method can be used to draw different geometric shapes such
as rectangle, triangle etc. The following example shows you how to draw a
rectangle using the Line method in Visual Basic 6.
Example:
Code:
Private Sub
cmdShow_Click()
DrawWidth = 3
Line (300, 300)-Step(4000, 2000), vbBlue, B
End Sub
The B argument in the Line method lets you draw a rectangle.
Output:
Displaying an image
The LoadPicture function sets a picture to the PictureBox control or
the form object. It requires the file path as an argument. The following
example shows you how to use the LoadPicture function.
Example:
Code:
Private Sub cmdShow_Click()
Picture1 = LoadPicture("D:\pic.JPG")
End Sub
Output:
Example:
Code:
New circles are drawn automatically when you resize the form. New
circles are drawn in random positions and with random sizes.
USING COLORS
Notice that all the graphics methods can use a Color argument. If that
argument is omitted, the ForeColor property is used. Color is actually a
hexadecimal (long integer) representation of color - look in the Properties
Window at some of the values of color for various object properties. So, one
way to get color values is to cut and paste values from the Properties
Window. There are other ways, though.
SYMBOLIC CONSTANTS:
Visual Basic offers eight symbolic constants (see Appendix I) to
represent some basic colors. Any of these constants can be used as a Color
argument.
QBCOLOR FUNCTION:
For Microsoft QBasic, GW-Basic and QuickBasic programmers, Visual
Basic replicates the sixteen most used colors with the QBColor function. The
color is specified by QBColor(Index), where the colors corresponding to the
Index are:
RGB FUNCTION:
The RGB function can be used to produce one of 224 (over 16
million) colors!
The syntax for using RGB to specify the color property is:
where Red, Green, and Blue are integer measures of intensity of the
corresponding primary colors. These measures can range from 0 (least
intensity) to 255 (greatest intensity). For example, RGB(255, 255, 0) will
produce yellow.
· Any of these four representations of color can be used anytime your Visual
Basic code requires a color value.
Color Examples:
Form1.BackColor = vbGreen
picExample.FillColor = QBColor(3)
label1.ForeColor = RGB(100, 100,
100)