home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Visual Basic 6 Black Book
(Publisher: The Coriolis Group)
Author(s): Steven Holzner
ISBN: 1576102831
Publication Date: 08/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


The With Statement

Properly speaking, the With statement is not a loop, but it can be as useful as a loop—and in fact, many programmers actually think of it as a loop. You use the With statement to execute statements using a particular object. Here’s the syntax:

With object
[statements]
End With

Here’s an example showing how to put With to work. Here, we use a text box, Text1, and set several of its properties in the With statement:

With Text1
    .Height = 1000
    .Width = 3000
    .Text = "Welcome to Visual Basic"
End With

Using Collections

Using collections, you can group related items together. Collections can be heterogeneous—that is, members of a collection don’t have to share the same data type, and that can be very useful, because life doesn’t always present you with collections made up of items of the same type.

You create a collection as you would any other object:

Dim GarageSaleItems As New Collection

You can add members to the collection with the Add method and remove them with the Remove method.

You can also reach specific members in the collection using the Item method. Most importantly, from a programming point of view, you can loop over the entire collection using the For Each…Next statement (see the previous section, “Looping”).

Collections are very useful and are one of the high points of Visual Basic. However, because of the heterogeneous nature of their contents, they don’t necessarily lend themselves to tight and uniform coding practices (which makes some C and C++ programmers look down their noses at Visual Basic).

Sending Keystrokes To Other Programs

It’s time to print out the 349 screen spreadsheets you’ve created in your new spreadsheet program to show the boss. Regrettably, there just doesn’t seem to be any way to print them out except one at a time, using the File menu’s Print item. Can Visual Basic help here?

Yes. You can use the SendKeys() function to send keys to the program that currently has the Windows focus, just as if you typed in those keys yourself. Using the Alt key, you can reach the menu items in your spreadsheet’s File menu. The day is saved, because now you can automate your printing job, even waiting until the spreadsheet program processes the current keystroke before continuing. Here’s how you use SendKeys():

SendKeys string [, wait]

The string expression is the string you want to send to the other program. The wait argument is a Boolean value indicating the wait mode. If False (which is the default), control returns right after the keys are sent. If True, the keystrokes must be processed by the other program before control returns.

If the keys you want to send are not simple text, just embed the codes you see in Table 3.7 in the text you send to SendKeys().

Table 3.7 SendKeys() key codes.
Key Code
Backspace {BACKSPACE}, {BS}, or {BKSP}
Break {BREAK}
Caps Lock {CAPSLOCK}
Del or Delete {DELETE} or {DEL}
Down arrow {DOWN}
End {END}
Enter or Return {ENTER} or ~
Esc {ESC}
Help {HELP}
Home {HOME}
Ins or Insert {INSERT} or {INS}
Left arrow {LEFT}
Num Lock {NUMLOCK}
Page Down {PGDN}
Page Up {PGUP}
Print Screen {PRTSC}
Right arrow {RIGHT}
Scroll Lock {SCROLLLOCK}
Tab {TAB}
Up arrow {UP}
F1 {F1}
F2 {F2}
F3 {F3}
F4 {F4}
F5 {F5}
F6 {F6}
F7 {F7}
F8 {F8}
F9 {F9}
F10 {F10}
F11 {F11}
F12 {F12}
F13 {F13}
F14 {F14}
F15 {F15}
F16 {F16}
Shift +
Ctrl ^
Alt %

Here’s an example showing how to use SendKeys(). Here, we give the Windows WordPad program the focus with the Visual Basic AppActivate() function, passing it the title of that program (which appears in its title bar), and send the string “Hello from Visual Basic!” to that program as follows:

AppActivate ("Document - WordPad")
SendKeys ("Hello from Visual Basic!")

The result appears in Figure 3.2—now we’re able to send keystrokes to another program.


Figure 3.2  Sending keystrokes to Windows WordPad.

Handling Higher Math

Well, it may have been a mistake taking on that programming job from the astrophysics department. How do you calculate a hyperbolic cosecant anyway? Can Visual Basic do it?

Yes, although not directly. The built-in Visual Basic math functions appear in Table 3.8.

Table 3.8 Visual Basic math functions.
Function Calculates This
Abs Absolute value
Atn Arc tangent
Cos Cosine
Exp Exponentiation
Fix Fix places
Int Integer value
Log Log
Rnd Random number
Sgn Sign
Sin Sine
Sqr Square root
Tan Tangent


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited.