Chapter 34. Using the Clipboard

Earlier lessons have explained how your program interacts with the user (through controls), the file system, and most recently the printer. This lesson explains how your program can interact with other programs via the clipboard.

In this lesson, you learn how to place text, image, and other data on the clipboard so you can paste it into other applications. You also learn how to receive data pasted into your program from the clipboard.

ADDING DATA TO THE CLIPBOARD

Adding data to the clipboard is fairly easy. The Clipboard class provides several static methods to place certain kinds of data on the clipboard. Table 34-1 summarizes those methods.

Table 34.1. TABLE 34-1

METHOD

PURPOSE

SetAudio

Copies an audio stream to the clipboard

SetFileDropList

Copies a list of files to the clipboard as if the user had selected them in Windows Explorer and pressed [Ctrl]+C

SetImage

Copies an image to the clipboard

SetText

Copies a string to the clipboard

Example program AddToClipboard, shown in Figure 34-1 and included as part of this lesson's code download, demonstrates these methods. Use the Copy buttons to copy the corresponding content on the left to the clipboard. (When the program starts, it copies the names of the files in its startup directory into the list box. The bottom image represents an audio file.)

FIGURE 34-1

Figure 34.1. FIGURE 34-1

The program uses the following code to copy data ...

Get Stephens' Visual Basic® Programming 24-Hour Trainer now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.