Академический Документы
Профессиональный Документы
Культура Документы
Programming - 5
A Specialised Training Course
Contents
APPLICATION MENUS ..................................................................................................................................... 2
MENU DESIGN DIALOG BOX............................................................................................................................. 3
Caption Text Box...................................................................................................................................... 3
Name Text Box ......................................................................................................................................... 3
Index Text Box.......................................................................................................................................... 3
Shortcut Combo Box................................................................................................................................. 3
WindowList Check Box............................................................................................................................. 3
HelpContextID Text Box .......................................................................................................................... 3
Checked Check Box.................................................................................................................................. 3
Enabled Check Box .................................................................................................................................. 4
Visible Check Box..................................................................................................................................... 4
Layout Section .......................................................................................................................................... 4
PROPERTIES ...................................................................................................................................................... 4
EVENT PROCEDURES ........................................................................................................................................ 4
FILE I/O - DOCUMENTS ................................................................................................................................... 5
SEQUENTIAL FILES ........................................................................................................................................... 6
RANDOM-ACCESS FILES ................................................................................................................................... 7
BINARY FILES ................................................................................................................................................... 8
COMMON DIALOG BOX CONTROL............................................................................................................. 9
Action Property ........................................................................................................................................ 9
DialogTitle Property ................................................................................................................................ 9
Filename Property.................................................................................................................................... 9
DefaultExt Property ................................................................................................................................. 9
Filter / FilterIndex Properties ................................................................................................................ 10
Flags Property........................................................................................................................................ 10
InitDir Property...................................................................................................................................... 10
DRIVE, DIRECTORY AND FILE CONTROLS ............................................................................................ 10
DRIVE LIST BOX CONTROL............................................................................................................................. 11
Drive property ........................................................................................................................................ 11
DIRECTORY LIST BOX CONTROL .................................................................................................................... 11
Path Property ......................................................................................................................................... 12
FILE LIST BOX CONTROL ................................................................................................................................ 12
Filename Property.................................................................................................................................. 12
Path and Pattern Properties................................................................................................................... 12
Normal, Archive, Hidden, ReadOnly and System Properties................................................................. 12
MultiSelect Property............................................................................................................................... 13
COMBINING THE CONTROLS ........................................................................................................................... 13
APPENDIX.......................................................................................................................................................... 14
Common Dialog Flag Property Constants............................................................................................. 14
EXERCISES........................................................................................................................................................ 15
Layout Section
This part allows you to layout the menu adding, levelling and deleting items from the whole
menu bar. The top half of the dialog box, described above, always shows the current settings
of the selected item from this section.
It comprises of a set of buttons and a list box that displays a hierarchical list of menu items.
Submenu items are indented to indicate their hierarchical position or level. Use the left and
right arrow keys to change the level of a menu item from a higher level to a lower level. You
can create up to four levels of submenus. Use the up and down arrow keys to change the
position of a menu item within the same menu level (up or down in the menu list).
The Next button moves selection to the next line, Insert inserts a line in the list box before
the currently selected line while Delete deletes the currently selected line.
Properties
All of the important properties have been described above and can be changed using the
Menu Design Dialog Box. Once the items have been created you can view the properties for
any menu item by using the drop list box at the top of the property window. Most can also be
changed at runtime.
Event Procedures
Each bottom level menu item can have a click_procedure defined for it. This can be quickly
accessed by ‘selecting’ the menu item at design time.
Function/ Description
Statement
Input$ Reads in a fixed number of characters from the file. If no. of characters is not
specified, it reads a whole file up to 65,535 characters long.
Input# Allows you to read data from a sequential file and place the values read into a
sequence of variables.
Line Input# Used to read data in a line at a time (i.e. until a Carriage return)
Write# / Used to write data from a list of expressions, in either comma-delimited (Write)
or space delimited format.
Print#
Several points must be remembered...
• To change between Input, Output and Append modes the file must be closed and
re-opened in the new mode.
• A file must exist before it is opened for Input or Append. Output mode creates a
new file or overwrites an old file.
• Although it is usual to read and write sequential files sequentially it is possible to
move the file pointer about using Seek
So, basically, you use Input when you want to read from the file, Output when you want to
write or overwrite part of the file and Append when you want to add to the file.
Example
This piece of code opens a file, the path and name of which is stored in the FileName string
variable. The text is read into a single text box.
...
Open FileName For Input As 1
If LOF(1) > 30000 Then
Msg = “The file is too large”
MsgBox Msg, 16, “File too Big”
Else
txtMainText.Text = Input$(LOF(1), 1)
End If
Close 1
...
If we are going to do record type work in sequential files it is worth using user-defined
data- types for brevity and accuracy.
Random-Access Files
This file type (otherwise known as simply Random) is generally used to store a series of
identical length records. Each record must be of a single data-type, but as we know we can
always create our own user-defined types. Technically you need to know the length of your
data types, but it is easier to use a function that works that out for you ‘LEN’. This
information is required when you open the file.
The main feature of this file access method is that you can read and write records wherever
they are in the file in a non-sequential manner; you can also mix reading and writing without
needing to change the mode of access.
The main statements we will use are...
Function/ Description
Statement
Open Allows you to open a Random file.
Open file for Random As filenumber Len=recordlength.
Close Closes an open file.
Get This copies a record from a specific position in the given file to the given
variable. e.g.
get filenumber, Position, Variable
Put This (over)writes a record to a specific position in the given file from a given
variable. e.g.
put filenumber, Position, Variable
Binary Files
The possibilities are endless, you can store the data in the manner you want. The only
problem is to make sure that you always read and write in a consistent manner as the
restrictions of the other types aren’t around to control matters.
If you do store record type information within binary files you have both the advantage and
disadvantage of variable length records. Variable length records can save space, but they
require a lot more ‘handling’.
You use Get and Put similar to Random files, but you can get and put any length variable at
any position.
Where this becomes useful is in applications where you need ‘header’ information contained
before other data. The other data can be text, bitmap graphic information, sound or anything.
The best way to keep track of this is to define a user defined data-type for your header
information which is always placed at the start of the file. This can be read in using one Get
statement, the rest of the data can then be read using a loop until the end of file is reached.
If you are using data files then you need some procedures for your user to open and save
them, but there are controls to help you select the file names, directories and drives.
Action Property
The type of dialog box displayed is determined by the Action property. The values of the
Action property and therefore the types of dialog boxes are...
Action Dialog Type
0 No Action
1 Open
2 Save As
3 Color (sic)
4 Font
5 Print
6 Invoke WINHELP.EXE
To display a Common Dialog Box at runtime you set the Action property to the type of
Dialog required. Other properties such as file name filters should be set before setting this
property.
DialogTitle Property
This determines the Title Bar text of the Dialog Box.
For now we will concentrate on properties relevant to file operations.
Filename Property
This is the property that actually captures the filename and path generated by the Open and
Save As boxes. This is then passed to the procedure that actually opens/saves the file.
DefaultExt Property
When using the Save As Dialog Box this defines the default file extension that is added to the
filename unless otherwise specified.
Flags Property
This property allows you to combine several constants as a single value to define various
options for the Open and Save As dialog boxes. The flag constants represent various bit
patterns that can be combined using the Or operator in a bit-wise context. See the table in the
Appendix for the full set.
An example of their use would be...
CMDialog1.Flags = OFN_SHOWHELP Or
OFN_HIDEREADONLY
or
CMDialog1.Flags = &H14& ‘ &H10& Or &H4&
which displays a help button in the associated dialog and hides the Read Only check box.
InitDir Property
Determines the drive and directory to default to.
As for its properties, it has List, ListIndex and ListCount properties that can be used like any
other list. It does have a vital property however...
Drive property
The property that actually stores the pertinent data this is a string property that specifies the
selected drive. It is only available at run-time.
By selecting a drive from the list the drive property contains the drive letter. Alternatively you
can set the drive by assigning it a string. Note that it only takes the first character, which is
useful in that we can assign a whole path string from which it picks the relevant information.
ChDrive drvSelectDrive.Drive ‘ Set
Current Drive to selection
drvSelectDrive.Drive = CurDir$ ‘Set
Control Drive to Current drive
The directory structure is represented graphically as well as textually; a small folder is placed
to the left of the directory name, and they are indented to show the parent~subdirectory
relationships. The directories can be browsed through by using the mouse to move up and
down the directory tree.
Filename Property
This is the important data property of the control. It contains not only a filename, but also the
full drive and directory path. This property can not be set at design time.
The possibility exists that using a text box or whatever we change the filename property of
the File list, including new drive and directory information. This automatically changes the
Path property of the File list. In this case we need to possibly update the other controls...
Sub filX_Change()
dirX.Path = filX.Path
drvX.Drive = filX.Path ‘takes first
letter
...
End Sub
3 In the code below you will notice a few extra statements that you haven’t yet come
across:
Dir - similar to the DOS command of the same name, it returns a string based on the
search criteria passed to it
Kill - deletes the file specified
LOF - ‘length of file’ returns the size of the file
Option Explicit
Why do you think that error checking is particularly important when dealing data files?
Add extra features to enable only the appropriate buttons when file exists/ not exists, etc.
4 Firstly the code that will set the text box to the same size as the form. We need to
activate this when we Load the Form and if we resize it. Place it in Form_Resize and
call it from Form_Load.
Sub Form_Resize ()
txtMainText.Left = 0
txtMainText.Top = 0
txtMainText.Width = frmTextEdit.Width
txtMainText.Height = frmTextEdit.Height
End Sub
Sub Form_Load ()
Form_Resize
End Sub
Sub mnuFileSave_Click ()
CMDialog1.DialogTitle = "Save
As..."
CMDialog1.Action = 2
Filename$ = CMDialog1.Filename
Open Filename For Output As 1
Print #1, txtMainText.Text
Close 1
End Sub
Sub mnuFileQuit_Click ()
End
End Sub
7 Save your files as TEXTEDIT.FRM then try out your new editor.
Note that, just by using the common dialog control we have made the program a lot
more intuitive to the user and reduced the amount of code we have to type (which is
always a good thing...)
8 Your turn. Develop your editor so that...
• A second menu heading captioned &Text is added with sub-menus &Bold and
&Italic. Try attaching sensible shortcut keys to these. Their code simply switches
bold and italic on and off in the text box. Make sure a check (tick) appears beside
the menu item when switched on.
Visual Basic Book 5 © Mark Collins 1999 Page 19
• How about inserting an Edit section after the File section. Build in some simple
cut, copy and paste using the text box Sel*** properties.
• Disable the Save As menu option if the text box is empty.
• Add extra code in the QueryUnload of the frmTextEdit to enable the user to save
any changes before exiting from the text editor.