Getting started with VB 4

Source: Internet
Author: User

Delete the Real-Time dish and use the UnLoad statement. For example, LoadNameArray (1 ).

For details about how to create a real-time menu, see section 4.3 of the tutorial.

5. CancelError attributes and Err objects in the general dialog box

When the CancelError attribute of the general dialog box is Tme, no matter when the "cancel" button is selected, a 32755 (cdlCancel) error is generated, that is, the Err Number attribute is set to 32755.

Err is a system object of VB. It records errors that occur during program running.

The important attributes of the Err Object include Number (default attribute) and Description. When an error occurs, the error generator stores the error Number and Description of the error in the Number and Description attributes respectively. For example, an error occurs when the "cancel" button is selected in the general dialog box with the CancelError attribute being True, the Number and Description attributes of the Err Object are set to 32755 and "select 'cancel '".

Various Errors often occur during the running of the program. For more information about error handling, see section 9.6 of the tutorial.

The following describes how to stop the program running when the program produces a cdlCancel error.

SubForm_ClickO

CommonDialogl. CancelError = True

On Error GoTo ErrorHandler

CommonDialogl. Action = l

'Statement for processing files

ErrorHandler:

If Err. Number = cdlCancel Then

End

Endlf

End Sub

6. Events related to Forms

Initialize and load events occur in sequence when you use the load statement for the first time to call the form (assuming that the form has not been created in the memory) into the memory. When unload is used to detach a form from the memory, the queryunload and unload events occur sequentially, and then the set form name = nothing statement is used to cancel the initialization.

Initialize is an event that occurs when the form is created. The initialize event is triggered only once throughout the lifecycle of the form. You can load a form into or delete it many times from the memory, but the form is created only once. That is to say, when the load statement is used to load the form into the memory, the load event is triggered, but not necessarily the initialize event.

After the unload statement is used to uninstall the form, if the set form name = nothing is not used to uninitialize the form, the initialize event will not be triggered the next time you use the load statement. Otherwise, the initialize event will occur.

Assume that there are two forms, forml and form2, and there are the following event processes:

'Forml form event Process

Sub form_click ()

Debug. Print "events that occur when the form is loaded into memory for the first time :"

Load form2

Debug. Print "events that occur when unload the form is used :"

Unloadform2

Debug. Print "events that occur when the second form is loaded into the memory :"

Load form2

Debug. Print "events that occur when unload the form is used :"

Unload form2

Debug. Print "events that occur when Initialization is lifted using the set form name = nothing statement :"

Set form2 = nothing

Debug. Print "events that occur when the form is loaded into the memory for the third time (after Initialization is removed :"

Load Form2

End Sub

'Form2 form event Process

Sub Form_Initialize ()

Debug. Print "Initialize event"

End Sub

Sub Form_Lond ()

Debug. Pnnt "Load event"

End Sub

Sub Form_QueryUnload (Cancel As hteger, UnloadMode As Integer)

Debug. Print "QueryUnload"

End Sub

Sub Form_Unlond (Cancel As Integer)

Debug. Print "Unload"

End Sub

Sub Form_Terminate ()

Debug. Print "Terminate"

End Sub

When you click Form1, the following information is displayed in the Debug window:

Events that occur when the form is transferred to memory for the first time:

Initialize event

Load event

Events that occur when Unload a form is used:

Query Unload

Unload

Events that occur when the second form is loaded into the memory:

Load event

Events that occur when Unload a form is used:

QueryUnload

Unload

Events that occur during initialization with the set form name = nothing statement:

Terminate

Events that occur when the form is loaded into the memory for the third time (after Initialization is removed:

Initialize event

Load event

7. Sequence of mousedown, mouseup, and c1ick events

When you press the mouse button on a form or control, the mousedown event is triggered. The mousedown event must be before the mouseup and c1ick events. However, the order in which mouseup and c-k events occur depends on the clicked object.

When you click a tag, text box, or form, the order is as follows:

(1) mousedown

(2) mouseup

(3) Click

When you click the command button, the order is as follows:

(1) mousedown

(2) Click

(3) mouseup

When you double-click a tag or text box, the order is as follows:

(1) mousedown

(2) mouseup

(3) Click

(4) dblcuck

(5) mouseup

Chapter 8 Documents

8.1 key knowledge points

1. File System Controls

The main attributes and events of the file system control provided by VB are shown in Table 2.9.1.

Table 2.9.1 main attributes and events of file system controls

Main Property events of the control

DriveListBox (drive) drive change

DirListBox (directory) Path Change

FileLiseBox (File) Path FileName Pattern Click DblClick

To organically associate the three controls, two event processes Drivel Change () and Dirl Change () must be used (assuming that the control uses the default name ):

To display a certain type of file, you must add a combo control and set the Pattern attribute of the file list box control.

2. Process file and directory (folder) Statements and functions

The file and directory (folder) Statements and functions provided by VB are shown in Table 2.9.2. VB6.0 provides more powerful File System Objects. For more information, see Chapter 7.

Table 2.9.2 file and directory statements and functions

The equivalent doscommand of VB statement and Function Form

FileCopy source, destination Copy file copying

Delete the Kill pathname DEL and ERASE files. A wildcard is available.

Name oldpathname As newpathname rename the rename File

The ChDrive drive keyboard character changes the current drive

MkDir path MD creates a new directory

ChDir path CD changes the current directory

RmDir path RD Delete directory

CurDir (drive) function returns the current path

[Note]

When you use the preceding statement to operate on a file, the file must be closed.

3. File Concept

File: a collection of data stored in external media identified by file names. A file consists of several records. One record can contain several data items. The file in the current operation has a pointer pointing to the current read/write location.

File classification: the stored content can be divided into program files and data files. The storage data can be divided into ASCII and binary files. The storage media can be divided into disk files and printed files: ordered files, random files, and binary files can be divided according to the organization and access form (This Chapter does not introduce these files because they are not used much ).

File read/write: The computer memory transfers data to external files. For writing files, the specified "Write statement" is used. The data in the external files is transferred to the memory, which is a read file, use the specified "read Statement ".

File Buffer: when reading and writing files, VB opens a "File Buffer" in the memory ", the content read from the file to the memory data area and written into the file from the memory data area must be sent to the buffer area first. The benefits of using "File Buffer" can increase the speed at which files can be read and written. An opened file corresponds to a buffer zone. Each buffer zone has a buffer zone number, that is, the file number involved later.

4. sequential files and operations

Sequential file: records are written into the file. The order of storage and reading is the same, that is, the logical order of records is the same as that of physical records. Ordered files store data in ASCII codes and can be displayed and edited by text editing software.

The structure of ordered files is simple, the records can be unequal, and the reading speed of a record is slow. Files cannot be directly modified, so it is suitable for batch processing of files.

Open statement format: Open file name For read/write mode As [#] File number (Len = record length]

The file name can be a String constant enclosed by double quotation marks, but it can also be a character variable (no "" At this time). The read/write methods include OUTPUT, INPUT, and APPEND. The file number is 1-511; the record Length indicates the number of characters in the buffer zone.

[Note]

When an existing file is opened in the OUTPUT mode, all the original data is deleted.

Write statement: Print # file number, [Output list] Or Write # file number, [Output list]

Difference between the two statements: the latter automatically inserts "," between data items and adds double quotation marks to the string to distinguish between data items and string types. For multiple data items, it can also be represented by a record variable name.

Read statement: Input # file number, variable list

Lineinput # file number, string variable (Note: Do not read the line break when you press ENTER;

Input $ (number of characters to be read, # file number) includes the number of characters to be read when the carriage return is a line break)

Close file: Close [[#] File number]

Common functions: LOF, eof, and loc.

5. Random files and operations

Random file: all records in the file are of the same length and the length of each data item is fixed. Each record has a unique record number. Read and Write files read and write the record according to the record number, files store data in the form of binary code. A random file is suitable for reading and writing a certain record.

Record: Generally, type... Define the endtype record type, and then declare the record variable.

Open statement format: open file name for random as # file number [Len = record length)

Record length: automatically obtained by Len (he recording type.

Write statement: Put [#] file number, [Record Number], variable list or record variable name

Read statement: Get [#] file number, [Record Number], variable list or record variable name

If the record number is omitted, a record is inserted or read after the current record.

9.3 Common Errors and difficulties

1. The three controls of the file system cannot be associated.

That is, when the drive changes, the directory list box cannot be changed accordingly; or when the directory list box changes, the file list box cannot be changed accordingly. To associate three controls, use the following two event processes:

Private sub drivel_change ()

Dirl. Path = drivel. Drive

End sub

Private sub dirl_change ()

Filel. Path = dirl. Path

End sub

2. How to display the currently selected directory in the directory list box

When the program is running, double-click a directory item in the directory list box to change the directory item to the current directory, and change its diirl. PATH value accordingly. The diirl. PATH value does not change when you click to select this directory. Sometimes, in order to perform related operations on the selected directory items, it corresponds to the selection of a list item in The ListBox control, which indicates the following:

Dirl. List (dirl. listlndex)

3. When the file system control is used to open the file, the error message "file not found" is displayed.

For example, the following statement:

Open filel. Path + filel. File name for input as #1

If the selected directory is the root directory, the preceding statement is correctly executed. If the selected directory is a subdirectory, the "found at the end of the file" error message is displayed when the preceding statement is executed.

Where: filel. Path indicates the selected path, and filel. File name indicates the selected file, which together indicates the object identifier.

When the selected file is in the root directory (assuming the drive is C), filel. the path value is "C:/1". If the selected file name is "t1.txt", then filel. path + filel. the filename value is "C:/t1.txt" and is a valid file identifier.

When the selected file is in a subdirectory (assuming the drive is C, the subdirectory is my), filel. the path value is "C:/my" filel. path + filel. the filename value is "C:/My tl.txt", and a "/" separator is missing between the subdirectory and the file name.

To ensure the normal operation of the program, change openfilel. Path + filel. filenameforlnput as #1:

Dun F $

If Right (Filel. Path, 1) "/" Then "indicates the selected root directory.

F = Filel. Path + Filel. FileName

Else 'indicates that the selected sub-directory is selected, and "/" is added between the sub-directory and the file name "/"

F = Filel. Path + "/" + Filel. FileName

End If

Open F For Input As #1

4. the name to be opened in the Open statement can be a constant or a string variable, but the user's concept is unclear, resulting in "file not found" error message

For example, if the file name is "C:/my/t1.txt ",

The correct constant is written as follows:

Oven "C:/my/t1.txt" For Input As #1 'incorrect writing constant with double quotation marks on both sides

Or the correct variables are written as follows:

Dim F $

F = "C:/my/t1.txt"

Open F For Input As # 1' incorrect writing variable F contains double quotation marks on both sides

5. The file is not closed and opened again. The error message "file opened" is displayed.

Statement:

Open "C:/my/t1.txt" For Input As #1

Print F

Open "C:/my/t1.txt" For Input As #1

Print "2"; F

When the second Open statement is executed, the error message "file opened" is displayed.

6. How to read all records in a random file without knowing the Record Number

If you do not know the record number and want to read all the records, as long as the reading of the files in the same sequence is similar, the Get statement that uses the loop structure and no record number is used is the following:

Do While Not EOF (1)

Get #1, j

Print j;

Loop

EndSub

When a random file is read or written, no record number is specified, indicating that the next record is automatically read during reading and inserted to the current record.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.