374
LESSON 32 Using the Clipboard
// Create the directory if necessary.
dirInfo.Create();
// Process the les.
foreach (string lename in Clipboard.GetFileDropList())
{
FileInfo leInfo = new FileInfo(lename);
// Add a version number to the end.
// Keep trying until we nd one that isn’t
// already there.
for (int i = 0; ; i++)
{
// Compose a le name in the backup directory
// with the le’s name and the version number.
string newFileName =
Path.Combine(dirInfo.FullName, leInfo.Name) +
“.” + i.ToString(“000”);
// See if this le exists.
if (!File.Exists(newFileName))
{
// Copy the le here.
leInfo.CopyTo(newFileName);
// Exit the for loop.
break;
}
}
}
}
MessageBox.Show(“Done”);
}
Please select Lesson 32 on the DVD to view the video that accompanies this lesson.
EXERCISES
1. Build the application shown in Figure 32-2. When the user presses
Ctrl+V, display any text or image that the clipboard contains.
Hints: Set the form’s KeyPreview property to true. Then use
code similar to the following to tell when the user presses
Ctrl+V. Notice that the code sets
e.Handled = true to indi-
cate that the key press can be discarded after the event handler
is done with it.
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
// See if it’s Ctrl+V.
FIGURE 322
596906c32.indd 374 4/7/10 12:34:36 PM
Exercises
375
if (e.Control && e.KeyCode == Keys.V)
{
...
// Mark the key press as handled.
e.Handled = true;
}
}
2. The Clipboard can contain several different kinds of text. Overloaded versions of the
ContainsText and GetText methods take a TextDataFormat parameter that indicates
the kind of text you want.
Build a program with two TextBoxes and a RichTextBox. When the user clicks a button,
see what kind of text is available and paste plain text into one
TextBox, HTML text into the
other, and rich text format (RTF) text into the
RichTextBox. Test the program by pasting text
copied from programs such as Microsoft Word and WordPad.
You can download the solutions to these exercises from the book’s web page at
www.wrox.com or www.CSharpHelper.com/24hour.html. You can find those
solutions in the Lesson32 folder.
596906c32.indd 375 4/7/10 12:34:36 PM
Click here to Play
596906c32.indd 376 4/7/10 12:34:36 PM
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.217.150.123