Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:

Schongar P.VBScript unleashed.1997

.pdf
Скачиваний:
45
Добавлен:
23.08.2013
Размер:
1.59 Mб
Скачать

IEPOP1.AddItem "New File"

IEPOP1.AddItem "Open File"

IEPOP1.AddItem "Save File"

IEPOP1.Popup

End Sub

The New File command simply clears the contents of the textbox and is implemented with a single statement:

RichTextbox1.Text=""

The code of the File Open option in the popup menu causes the Open File Common Control dialog box to be displayed with the following lines:

CommonDialog1.Action=1

RichTextBox1.LoadFile CommonDialog1.FileName, 0

And the code of the File Save option is just as simple:

CommonDialog1.Action=2

RichTextBox1.SaveFile CommonDialog1.FileName, 0

To add file-handling capabilities to your application, insert the following Case statement in the existing Select Case structure:

Case "FILE":

If item=1 Then RichTextbox1.Text=""

If item=2 Then

CommonDialog1.Action=1

RichTextBox1.LoadFile CommonDialog1.FileName, 0

End If

If item=3 Then

CommonDialog1.Action=2

RichTextBox1.SaveFile CommonDialog1.FileName, 0

End If

The implementation of the file-related commands is quite simple, but it should be a bit lengthier. For example, what will happen if the user clicked the Cancel button? The Common Dialogs control returns an empty string. Here's a better way to implement the File Open command:

If item=2 Then

CommonDialog1.Action=1

Filename=CommonDialog1.FileName

If FileName="" Then Exit Sub

RichTextBox1.LoadFile CommonDialog1.FileName, 0

End If

Similar changes must be made to the File Save command's code so that the program will not attempt to save the current document under an invalid filename. In the case of the File Save command you can also prompt the users with an Input Box as to whether they want to save the current document in RTF (filetype=0) or text (filetype=1) format.

Safety Considerations

As we mentioned earlier, Internet Explorer will issue a warning every time it's about to load a page with an OCX control. OCX controls aren't unsafe for the client system, but they are not ActiveX controls, and they haven't been verified for security. The RTFEditor application could be considered doubly unsafe because it saves data on the local disk. People are very reluctant to allow an application that they ran into on the Internet to leave anything on their disk. Applications that make use of OCX controls are quite safe for an intranet environment, but you should think twice before placing them on public Web pages. When the ActiveX versions of these controls become available the situation will change. It's not known how these controls will handle security, but some mechanisms will be built into the controls to protect the viewers. Even a simple warning will help because the user will be able to control which application saves data to the local disk, and when.

Review

Until all the existing controls are converted to ActiveX format, you can use the OCX controls to develop quite powerful utilities for the Web pages you plan to post on your intranet. The RichTextbox control, for example, provides all the functionality you need to build a simple word processor for everyone in the company. If you plan to use OCX controls on your pages, you should consider purchasing Visual Basic and consult the documentation for additional features of the various OCX controls. The Rich Textbox control, for example, provides methods for searching the text on the control, various ways of selecting text (from the location of the pointer to the end of the line, for example), a method for printing the text, and so on. One final suggestion is to customize each user's editor with cookies. For instance, you can provide a first-level menu that lets the users select the type of document they want to create, and you can bring up the RTFEditor application with a predefined template document, the user's settings (colors, font), the user's name, the current date, and so on. There are Microsoft Office applications that will do all that, but right now none of them can be invoked and customized from within a Web page as easily as the RTFEditor application-not to mention that the average user will find a custom editor like this one easier to learn and use.

Of course, the users of your applications that make use of regular OCX controls must be aware of the warning that

Internet Explorer will issue and ignore it. This is more likely to happen in an intranet environment rather than the World Wide Web. We expect to see ActiveX versions of the OCX controls discussed in this and the following chapter shortly after this book hits the market.

The next chapter continues the exploration of OCX controls and how they can be used in Internet Explorer's environment by showing you how to build a spreadsheet application. The GraphData application uses the Grid OCX control to let the user enter data on a spreadsheet and then plots the data on the Graph control (which is actually an ActiveX control).

Appendix A

VBScript Language Reference

CONTENTS

Variables, Constants, and Expressions

Operators

Arithmetic Operators

Concatenation Operators

Logical Operators

Comparison Operators

Statements

Functions

Variable and Conversion Functions

Date/Time Functions

Conditional Functions

String Functions

Input Functions

Mathematical Functions

Variables, Constants, and Expressions

Variables and constants are similar in that they both refer to a location in memory that contains a value. A constant has a value that remains the same throughout the execution of the program. A variable, on the other hand, is modified during the execution. Every constant and variable is assigned a name that uniquely identifies it and must follow the conventions listed here:

Must begin with an alphabetic character

Cannot contain an embedded period or type-declaration character

Must be unique in the same scope

Must not exceed 255 characters

A constant may be a string or numeric literal, another constant, or any combination that includes arithmetic or logical operators. For example:

Const Ident = "This is Freds script"

Possible variable subtypes as well as their data ranges are shown in Table A.1. Other reserved terms when describing data types are shown in Table A.2.

 

Table A.1. Variant subtypes.

Subtype

Range

Byte

0 to 255.

Boolean

True or False.

Integer

-32,768 to 32,767.

Long

-2,147,483,648 to 2,147,483,647.

Single

-3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45

 

to 3.402823E38 for positive values.

Double

-1.79769313486232E308 to -4.94065645841247E-324 for negative

 

values; 4.94065645841247E-324 to 1.79769313486232E308 for

 

positive values.

Currency

-922,337,203,685,477.5808 to 922,337,203,685,477.5807.

Date

January 1, 100 to December 31, 9999, inclusive.

Object

Any Object reference.

String

Variable-length strings may range in length from 0 to approximately

 

2 billion characters (approximately 65,535 for Microsoft Windows

 

version 3.1 and earlier).

 

Table A.2. Data type reserved terms.

Term

Description

Nothing

A value that indicates that an object variable is no longer associated

 

with any actual object.

Null

A value indicating that a variable contains no valid data.

Empty

A value that indicates that no beginning value has been assigned to a

 

variable (0 for a numeric subtype or a zero-length string).

Operators

When several operations occur in an expression, each part is evaluated and resolved in a predetermined order known as operator precedence. (See Table A.3.) Parentheses can be used to override this order. When the precedence is equal (that is, addition and subtraction, multiplication and division, all comparison operators), the expressions are evaluated from left to right.

 

Table A.3. Operator precedence.

Symbol

Description

Type

^

Exponentiation

Arithmetic

-

Negation

Arithmetic

*

Multiplication

Arithmetic

/

Division

Arithmetic

\

Integer division

Arithmetic

Mod

Modulo arithmetic

Arithmetic

+

Addition

Arithmetic

-

Subtraction

Arithmetic

&

String concatenation

Arithmetic

=

Equality

Comparison

<>

Inequality

Comparison

<

Less than

Comparison

>

Greater than

Comparison

<=

Less than or equal to

Comparison

>=

Greater than or equal to

Comparison

IS

Same object

Comparison

NOT

Negation

Logical

AND

Bitwise conjunction

Logical

OR

Bitwise disjunction

Logical

XOR

Bitwise exclusion

Logical

EQV

Bitwise equivalence

Logical

IMP

Bitwise implication

Logical

Arithmetic Operators

Arithmetic operators are used in expressions to perform mathematical calculations. The following general rules are involved in the operations:

If one or both operands are Null expressions, the result is Null.

If an operand is Empty, it is treated as a 0.

+(Addition) Operator

This operator determines the sum of two numbers. Its usage is

sum = expr1 + expr2

where sum is a numeric variable, and expr1 and expr2 are any expressions.

NOTE

Though the + operator can be used to concatenate two character strings, its usage as a concatenation operator is discouraged. The & operator should be used for that purpose.

If both expressions in the operation are strings, a concatenation of these strings will occur. Otherwise, an addition will be performed.

The general rules apply. If both expressions are Empty, the result is an Integer subtype equal to 0.

- (Subtraction) Operator

The subtraction operator will yield the difference between two numbers when used as

result = number1 - number2

where result is a numeric variable, and number1 and number2 are numeric expressions.

This operator can also be used as the unary negation operator to change to the negative value of a numeric expression. In this case its usage is

-number

where number is a numeric expression.

The general rules apply.

* (Multiplication) Operator

The multiplication operator will yield the product of two numbers. Its usage is

result = multiplier1 * multiplier2

where result is a numeric variable, and multiplier1 and multiplier2 are numeric expressions.

The general rules apply.

/ (Division) Operator

The division operator will yield the quotient of two numbers. Its usage is

quotient = dividend / divisor

where quotient is a numeric floating-point variable, and dividend and divisor are numeric expressions.

The general rules apply.

\ (Integer Division) Operator

The integer division operator divides two numbers and return an integer result. Its usage is

quotient = dividend \ divisor

where quotient is a numeric variable, and dividend and divisor are numeric expressions.

Numeric expressions are rounded to Byte, Integer, or Long subtype expressions. Then the general rules apply.

^ (Exponentiation) Operator

The exponentiation operator will yield the power of a number raised to an exponent. Its usage is

result = number ^ exponent

where result is a numeric variable, and number and exponent are numeric expressions.

As well as the general rules, exponent must be an integer if number is a negative value.

Mod (Modulus) Operator

The modulus operator determines the remainder from the division of two numbers. Its usage is

result = dividend Mod divisor

where result is any numeric variable, and dividend and divisor are numeric expressions. If the dividend or divisor is a floating-point number, it is rounded to an integer before the operation. The general rules apply.

Concatenation Operators

Concatenation operators combine strings. The general rules are

Any non-string operands are converted to a String subtype before the operation.

Any operand that is Null or Empty is treated as a zero-length string.

If both operands are Null, the result is Null.

&(Concatenation) Operator

The & concatenation operator will combine two expressions into a string result. Its usage is

result = expr1 & expr2

where result is any variable, and expr1 and expr2 are any expressions.

+ (Concatenation) Operator

The + concatenation operator functions in the same manner as the & concatenation operator when either operand is a String subtype. Its usage is

result = expr1 + expr2

where result is any variable, and either expr1 or expr2 is a String variable.

NOTE

Use of the + concatenation operator is not recommended. Use the & operator to eliminate ambiguity.

Logical Operators

Logical operators perform logical comparison and algebraic bitwise operations. Some general rules are

A Null expression is treated as a numeric zero in these operations.

A nonzero value (or bit) is True.

A zero value (or bit) is False.

AND Operator

The AND operator can perform a logical conjunctive comparison on two expressions as well as perform a bitwise algebraic conjunctive operation. Its usage is

result = expr1 AND expr2

where result is any numeric variable, and expr1 and expr2 are any expressions. When the AND operator performs a bitwise comparison of two numeric expressions, it sets the corresponding bit in result according to the truth table in Figure A.1. When used as a logical comparison operator, result is set according to the truth table in Figure A.1 also.

Figure 1. : AND operator truth tables.

OR Operator

The OR operator can perform a logical disjunctive comparison on two expressions as well as perform a bitwise algebraic disjunctive operation. Its usage is

result = expr1 OR expr2

where result is any numeric variable, and expr1 and expr2 are any expressions. When the OR operator performs a bitwise comparison of two numeric expressions, it sets the corresponding bit in result according to the truth table in

Figure A.2. When used as a logical comparison operator, result is set according to the truth table in Figure A.2 also.

Figure 2. : OR operator truth tables.

XOR Operator

The XOR operator can perform a logical exclusive comparison of two expressions as well as perform a bitwise exclusive algebraic operation. Its usage is

result = expr1 XOR expr2

where result is any numeric variable, and expr1 and expr2 are any expressions. When the XOR operator performs a bitwise comparison of two numeric expressions, it sets the corresponding bit in result according to the truth table in Figure A.3. When used as a logical comparison operator, result is set according to the truth table in Figure A.3 also.

Figure 3. : XOR operator truth tables.

EQV Operator

The EQV operator can perform a logical equivalence comparison of two expressions as well as perform a bitwise equivalence algebraic operation. Its usage is

result = expr1 EQV expr2

where result is any numeric variable, and expr1 and expr2 are any expressions. When the EQV operator performs a bitwise comparison of two numeric expressions, it sets the corresponding bit in result according to the truth table in Figure A.4. When used as a logical comparison operator, result is set according to the truth table in Figure A.4 also.

Figure 4. : EQV operator truth tables.

IMP Operator

The IMP operator can perform a logical implication comparison of two expressions as well as perform a bitwise implication algebraic operation. Its usage is

result = expr1 IMP expr2

where result is any numeric variable, and expr1 and expr2 are any expressions. When the IMP operator performs a bitwise comparison of two numeric expressions, it sets the corresponding bit in result according to the truth table in Figure A.5. When used as a logical comparison operator, result is set according to the truth table in Figure A.5 also.

Figure 5. : EQV operator truth tables.

- (Negation) Operator