The System Objects

Unlike the objects you declare, system objects are the Printer and App objects you've already used in this book. Although you can't pass system objects (they're already global in nature), you can treat them much like the objects you create. System objects represent specific elements of your application.

Table 16.1 describes the system objects and lists some important methods you can apply to them.

Table 16.1. System objects support several methods.

System Object Methods Description Methods
App   The current application.
  EXEName Returns the application's filename.
  Path Returns the application's path.
  Title Returns the primary startup form's title bar text.
  Previnstance Returns True or False, indicating whether another copy of the application is now running.
Clipboard   The Windows Clipboard region.
  Clear Erases the Clipboard.
  GetData Returns the graphic image stored on the Clipboard.
  GetFormat Returns the format of the Clipboard object.
  GetText Returns the text from the Clipboard.
  SetData Copies a graphic image to the
    Clipboard.
  SetText Copies text to the Clipboard.
  SelStart Used for Clipboard selection
    operations.
  SelLength Used for Clipboard selection operations.
  SelText Used for Clipboard selection operations.
Debug   The Immediate window.
  Printmethod: Copies information at runtime to the Immediate window (only possible in non-EXE Visual Basic programs that are run from the development environment).
Err   Holds information about current error status of an application.
  Key property Number holds an error code that corresponds to the most recent system error. (Holds zero if no error has occurred.)
Printer   The system printer. Day 13 introduced the methods of the Printer object and demonstrated how they provide printer support.
  Screen The user's screen.
  FontCount Returns the number of fonts the current screen supports.
  Fonts Contains a list of all the screen's possible font names.
  Height Returns the height of the screen area in twips.
  MousePointer Holds the shape of the mouse cursor or determines its shape if you specify a new mouse cursor.
  TwipsPerPixelX Returns the number of possible horizontal twips.
  TwipsPerPixelY Returns the number of possible vertical twips.
  Width Returns the width of the screen in twips.

You've worked with most of the system objects - especially the Printer and Screen objects - before today's lesson. The App object is useful for determining runtime information about the program path and filename, and the Clipboard object provides some interesting functionality you may want to use. Also, the Debug object lets you interact with your program during testing to help get the bugs out.

Depending on your application's needs, the Clipboard object is relatively simple to program. The Clipboard object is the same Clipboard Windows uses; therefore, your application can copy or cut information to the Clipboard object, and users can paste that information in another Windows application. Also, your application can paste information that's contained in the Windows Clipboard into the Clipboard object.

You can use the Clipboard object and its properties to select text from within your program and to determine the text selected by users. For example, the SelStart property marks the starting position of the selection cursor in the text box (or whatever control receives the selection). A value of 0 for SelStart places the cursor before the first character. SelLength determines how many characters are selected for Clipboard work. If you select text by setting SelStart and SelLength values, that text goes to the Clipboard object when a user presses Ctrl+C (copy) or Ctrl+X (cut). SelText is a string that contains the selected text you've bounded with SelStart and SelLength.

If you've selected text in a text box control (or you've asked a user to select text), that text appears in the SelText string value. You can clear the Clipboard object of its current value (the Clipboard object can hold only one value at a time) and send the selected text to the Clipboard with the following code:

    Clipboard.Clear ' Erase current Clipboard
    Clipboard.SetText txtName.SelText ' Copy text
    

If you want to copy the Clipboard text into a variable, you can use GetText(), like this:

strInfo = Clipboard.GetText()

If you want to replace selected text in a control with the text in the Clipboard object, you can do so this way:

txtName.SelText = Clipboard.GetText()

The GetText() method sometimes uses arguments, and it requires parentheses even if you specify no arguments. For text-based Clipboard work, you don't need to supply any arguments.

Top Home