Beruflich Dokumente
Kultur Dokumente
INTRODUCTION:
The file system controls helps the user in understanding the methodology of
using them for choosing disk files for his application. It also gives a detailed description
of accessing files in visual basic .A few windows features such as Clipboard is also
dealt with here.
There are three file system controls in visual basic .they are DirListBox,
FileListBox, and DriveListBox. In a application that allows to selecting files from drive
from drives these three controls are sued in combinations. These three controls having
Common properties and Methods:
Property Description
Enabled Specifies whether or not the user is allowed to interact with the
control.
List Array that stores the strings that appear in the control.
ListCount Number of items in the list property array.
ListIndex Index of selected list property item
MousePointer Specifies the shape of the mouse pointer
Visible Specifies whether or not the control is visible.
Methods:-
Drag Starts, Terminates, or Aborts Drag Operations.
Refresh Forces the control to repaint itself.
SetFocus Transfers the focus to the control.
Some FileListBox Properties:-
Property Description
Archive Specifies whether or not archive attributes are displayed
Hidden Specifies whether or not hidden attributes are displayed
MultiSelect Specifies whether or not the user can make multiple selections
Path Specifies the current path
Pattern Specifies the type of files displayed in the FileListBox
ReadOnly Specifies whether or not read-only attributes are displayed
System Specifies whether or not system or not system attributes are
displayed.
Data Hierarchy:-
The “Data” items processed by a computer are reduced to combinations of zeros and
ones. Such data item is called “Bit” such data in lower level means Decimal digits,
letters and characters.
A “Field” is a group of characters that conveys meaning.
Data items processed by computer form a data hierarchy in which data items becomes
more complex in structure as we progress form bits.
A “Record” is a composed of several fields (also called members) in a payroll system. A
Record is a group of related fields.
A “File” is a group of related records.
A “Record Key” identifies a record as belonging to a particular person or entity that is
unique from all other records in the file.
The “Records” which are typically stored in order by the record key field.
A group of related file is sometimes called a “DataBase”
A Collection of programs designed to create and manage databases is called a
“Database Management System”(DBMS).
The Below Example Shows Data Hierarchy.
FileSystemObject:-
Methods Description
CopyFile Copies an existing “File”
CopyFolder Copies an existing “Folder”
CreateFolder Creates and Returns Folder
CreateTextFile Creates and Returns File
DeleteFile Deletes a File
DeleteFolder Deletes a Folder
DriveExists Tests whether or not a Drive exists.
FileExists Tests whether or not File exists.
FolderExists Tests whether or not Folder exists.
GetAbsolutePathName Returns the absolute path as a String.
GetDrive Returns the specified Drive
GetDriveName Returns the Drive Drivename
GetFile Returns the specified File
GetFileName Returns the file filename
GetFolder Returns the specified Folder
GetParentFolderName Returns a String representing the parent folder name
GetTempName Creates and returns a String representing a file name
MoveFile Moves a File
MoveFolder Moves a Folder
OpenTextFile Opens an existing text File. Returns a TextStream.
Syntax:
Dim fso as New FileSystemObject
Dynamically creates “FileSystemObject” for fso using the keyword “New”.
The object now exists in memory like variable,objects have scope and fso is a module
variable.
File:-
Which allows the programmer to gather information about files, manipulate
files and open files.
File Properties & Methods:-
Properties/Methods Description
Properties:-
DateCreated The date the File was created
DateLastAccessed The date the File was last accessed
DateLastModified The date the File was last modified.
Drive The Drive where the file is located
Name The File Name
ParentFolder The File’s parent folder Name
Path The File’s path
ShortName The File’s Name expressed as a short name
Size The size of the File in bytes
Methods:-
Ex:-
Dim f as File
Set f= fso.GetFile(“d:\mpcs.txt”)
Folder:-
Folder objects allows the programmer to gather information about folder and to
manipulate folder.
Folder Properties & Methods:-
Properties/Methods Description
Properties:-
Attributes Value corresponding to folder’s attributes
DateCreated The date the Folder was created
DateLastAccessed The date the Folder was last accessed
DateLastModified The date the Folder was last modified.
Drive The Drive where the folder is located
IsRootFolder Indicates whether or not a Folder is the root folder
Name The Folder name
ParentFolder The Folder’s parent folder Name
Path The Folders’s path
ShortName The Folder’s Name expressed as a short name
ShortPath The Folder’s Name expressed as a short path
Size The size of the File in bytes
Type The Folder Type.
Methods:-
Ex:-
Dim fld as Folder
Set fld= fso.GetFolder(path)
Drive:-
Drive Objects allows the programmer to gather information about drives.
Drive Properties:-
Properties Description
Properties:-
AvailableSpace The Amount of available Drive space in bytes
DriveLatter The Letter assigned the Drive
DriveType The Drive type
FileSystem The File system Drive Description.
FreeSpace Same as AvailableSpace
IsReady Indicates whether or not Drive is ready for use
Path The Drive’s path
RootFolder The Drive’s root folder
SerialNumber The Drive volume name
TextStream:-
Allows the programmer to read and write text files. When working with sequential files
the programmer uses the File System Object “TextStream” to read and write file data. A
Text Stream is a stream of characters that are read from a file or written to a file
TextStream Properties & Methods:-
Properties Description
Properties:-
AtEndOfLine Specifies if the position is at the end of the line
AtEndOfStream Specifies if the position is at the end of the stream
Column The current character’s position in a line
Line The current line Number
Methods:-
Accessing FILES
A file consists of series of related bytes laocated in a Disk. When an applicatioin
accesses a file, it must assume what the bytes are supposed to represent. Depending on
the kind of data the file contains, we can use the appropriate file access type. There are
three ways of accessing files in visual basic. They are as follows
Sequential access
Random access
Binary access
Creating a Sequential Access File:-
Sequential Access is designed for use with plain text files. Each character in the
file is assumed to represent either a text character or a text formatting sequence, such as
a new line character NL. Data is stored as ANSI characters. From a Sequential access
file, we can read the contents of that file only Sequentially.
Steps for creating Sequential Access File
To Create a Sequential access file , we use TextStreamObject, which is an Object of
FileSystemObjects.
The following program creates a sequential access as shown below.
Reading a Sequential Access File:-
Ex:
Updating a Sequential Access Files:-
Records as User-DefinedTypes :-
Visual Basic provides programmers with the ability to create “User Defined
Types”. User defined types are collections of related variables. Userdefined types may
contain variables of many different data types in constrast to arrays that contain only
elements of the same datatype. Userdefined datatype are commonly used to define
fixed length records to be stored in random access files.
User defined types are derived datatypes they are constructed using objects of
other types. Consider the following datatype definition.
Ex: Private Type Student
Sno as integer
Sname as String*10
Marks as String
End Sub
Keyword “Type” introduces the user defined definition. Identifier “Student” is
the Typename. The Type name is used to declare variables of the userdefined type.
“Type” definition must be “private” when declaring in form modules,but can be
declared as “public” or “Private” in standard code module or class module
Varibles declared in the “Type” definitions are called the “type’s member” members of
same Type must have unique names.
The preceding “Type” definition does not reserve any space in memory, rather the
definition creates a new datatype that is used to declare variables. “Type” Variables are
declared like variables of other types. The declaration is
Binary access file are accessed byte by byte.once a file is opened for bunary access we
can read from and writes to any location in the file. The ability to access any desired
byte in the file makes it the most flexible one. Before accessing a file in binary mode,
the file should be opened first for binary access.
Example:
A new project is opened and the Form of the saved as BIN.frm and the project file as
BIN.BAT
A ComandButton is added and the following code is entered in the Click event.
Filenum=FreeFile
Open “C:\ BINARY.Dat” For Binary As filenum
End Sub
The application is executed and the click event of the CommandButton creates the
file BINARY.Dat .The Put statement takes three parameters. The first parameter is the
file number, the second is the byte location where the writing starts and the third is the
names of the variables whose contents will be written into the file.
Interface with Windows:
Visual Basic is a Windows programming language and hence let us discuss about a
few Windows Features such as Clipboard and the various keyboard events for detecting
whether the key was pressed or released
Example:
A new Standard EXE project is opened and a TextBox is drawn on the Form.
The MultiLine property of the TextBox is set to True and Its Left and Top properties
to 0(zero).
Menu items are added to the Form
Caption Name
&File mnFile
…. Cu&t muCut
…..Copy muPaste
……&Paste muPaste
The TextBox fills the entire area of the Form. This is done by changing the Width and
Height properties of the TextBox to ScaleWidth and ScaleHeight property of the Form
The following code is entered in the mnuCut_Click ()
Private sub mnuCut_Click()
Clipboard. Clear
Clipboard.SetText Text1.SelText
Text1.SetText = “ “
End sub
The following code is entered in the mnuCopy_Click () procedure.
It copy the highlighted text in to the Clipboard.