1. Trang chủ >
  2. Công Nghệ Thông Tin >
  3. Quản trị mạng >

Figure 3-3. A UserForm dialog box

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (5.72 MB, 490 trang )

For instance, the following is the code for the Close button's Click event in Figure 3-3. Note that

the Name property of the command button has been set to cmdClose :

Private Sub cmdClose_Click()

Unload Me

End Sub

All this code does is unload the form.

Along with event code for a form and its controls, we can also include support procedures within

the UserForm object.

Don't worry if all this seems rather vague now. We will devote an entire chapter to creating

custom dialog boxes (that is, UserForm objects) later in the book and see several real-life

examples throughout the book.

3.2 The Properties Window




The Properties window (see Figure 3-1) displays the properties of an object and allows us to

change them.

When a standard module is selected in the Project window, the only property that appears in the

Properties window is the module's name. However, when a workbook, sheet, or UserForm is

selected in the Projects window, many of the object's properties appear in the Properties window,

as shown in Figure 3-4.


The Properties window can be used to change some of the properties of the object while no code is

running—that is, at design time. Note, however, that some properties are read-only and cannot be

changed. While most properties can be changed either at design time or run time, some properties

can only be changed at design time and some can only be changed at run time. Run-time

properties generally do not appear in the Properties window.

Figure 3-4. The Properties window

17 ®


3.3 The Code Window

The Code window displays the code that is associated with the selected item in the Project

window. To view this code, select the object in the Projects window and either choose Code from

the View menu or hit the F7 key. For objects with only a code component (no visual component),

you can just double-click on the item in the Projects window.

3.3.1 Procedure and Full-Module Views

Generally, a code module (standard, class, or UserForm) contains more than one procedure. The

IDE offers the choice between viewing one procedure at a time (called procedure view) or all

procedures at one time (called full-module view), with a horizontal line separating the procedures.

Each view has its advantages and disadvantages, and you will probably want to use both views at

different times. Unfortunately, Microsoft has not supplied a menu choice for selecting the view.

To change views, we need to click on the small buttons in the lower-left corner of the Code

window. (The default view can be set using the Editor tab of the Options dialog box.)

Incidentally, the default font for the module window is Courier, which has a rather thin looking

appearance and may be somewhat difficult to read. You may want to change the font to FixedSys

(on the Editor Format tab of the Options dialog, under the Tools menu), which is very readable.

3.3.2 The Object and Procedure List Boxes

At the top of the Code window, there are two drop-down list boxes (see Figure 3-1). The Object

box contains a list of the objects (such as forms and controls) that are associated with the current

project, and the Procedure box contains a list of all of the procedures associated with the object

selected in the Object box. The precise contents of these boxes varies depending on the type of

object selected in the Project Explorer.

18 A workbook or sheet object

When a workbook or sheet object is selected in the Project window, the Object box contains only

two entries: general, for general procedures, and the object in question, either Workbook,

Worksheet, or Chart. When the object entry is selected, the Procedure box contains empty code

shells for the events that are relevant to that object. Figure 3-5 shows an example.

Figure 3-5. The events for a workbook object

If, for example, we choose the BeforeClose event in the Procedures box, Excel will create the

following code shell for this event and place the cursor within this procedure:

Private Sub Workbook_BeforeClose(Cancel As Boolean)

End Sub A standard module

When a standard module is selected in the Project window, the Object box contains only the entry

General, and a Procedure box lists all of the procedures we have written for that module (if any).

Figure 3-6 shows the open Procedure box, with a list of the current procedures for a particular

module. The Declarations section is where we place variable declarations for module-level

variables —that is, for variables that we want to be available in every procedure within the

module. We will discuss this in detail in Chapter 5.

Figure 3-6. The Procedure box A UserForm object

When a UserForm object is selected in the Project Explorer, the Object box contains a list of all of

the objects contained in the UserForm. For instance, Figure 3-7 shows the contents of the Object

box for the UserForm object in Figure 3-3. Note that there are entries for the various command

buttons (such as cmdClose), the various other controls, and even for the UserForm itself.


Figure 3-7. The Object box

Figure 3-8 shows the contents of the Procedure box when the cmdClose object is selected in the

Object box. This list contains the names of the 13 different events that are associated with a

command button.

Figure 3-8. The Procedure box

For example, if we select Click, we will be placed within the Code window between the following

two lines, where we can write event code for the Click event of the cmdClose command button :

Private Sub cmdClose_Click()

End Sub

3.4 The Immediate Window

The Immediate window (see Figure 3-1) has two main functions. First, we can send output to this

window using the command Debug.Print. For instance, the following code will print whatever

text is currently in cell A1 of the active worksheet to the Immediate window:

Debug.Print ActiveSheet.Range("A1").Text

This provides a nice way to experiment with different code snippets.

The other main function of the Immediate window is to execute commands. For instance, by

selecting some text in the active document, switching to the Immediate window, and entering the

line shown in Figure 3-9, the selected text will be boldfaced (after hitting the Enter key to execute

the code).


Xem Thêm
Tải bản đầy đủ (.pdf) (490 trang)