|
|
![]() | |
|
|
|
To access the contents, click the chapter and section titles.
Visual Basic 6 Black Book
The With Statement Properly speaking, the With statement is not a loop, but it can be as useful as a loopand in fact, many programmers actually think of it as a loop. You use the With statement to execute statements using a particular object. Heres the syntax: With object [statements] End With Heres 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 CollectionsUsing collections, you can group related items together. Collections can be heterogeneousthat is, members of a collection dont have to share the same data type, and that can be very useful, because life doesnt 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 dont 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 ProgramsIts time to print out the 349 screen spreadsheets youve created in your new spreadsheet program to show the boss. Regrettably, there just doesnt seem to be any way to print them out except one at a time, using the File menus 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 spreadsheets 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. Heres 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().
Heres 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.2now were able to send keystrokes to another program.
Handling Higher MathWell, 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.
|
|
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. |