For security purposes, access to the clipboard is only allowed through a user-initialed event. For example, you couldn't have a timer running in the background to constantly get text from the clipboard.
For first, let's create a Silverlight 4 application and name it SL4ClipboardAccess. Then Click OK on the next popup.

I added some controls to the page..
<Grid x:Name="LayoutRoot" Background="White">
<TextBox
Name="CopyTextTextBox"
Height="23"
HorizontalAlignment="Left"
Margin="132,36,0,0"
VerticalAlignment="Top"
Width="174" />
<TextBlock
Name="textBlock1"
Text="Text To Copy:"
Height="23"
HorizontalAlignment="Left"
Margin="47,40,0,0"
VerticalAlignment="Top" />
<Button
Name="CopyToClipboardButton"
Content="Copy To Clipboard"
Click="CopyToClipboardButton_Click"
Height="39"
HorizontalAlignment="Left"
Margin="144,81,0,0"
VerticalAlignment="Top"
Width="145" />
<Button
Click="PasteToTextBoxButton_Click"
Name="PasteToTextBoxButton"
Content="Paste To Textbox"
Height="39"
HorizontalAlignment="Left"
Margin="144,147,0,0"
VerticalAlignment="Top"
Width="145" />
<TextBox
Name="PasteTextTextBox"
Height="23"
HorizontalAlignment="Left"
Margin="132,209,0,0"
VerticalAlignment="Top"
Width="174" />
<TextBlock
Name="textBlock2"
Text="Text From Clipboard:"
Height="23"
HorizontalAlignment="Left"
Margin="8,209,0,0"
VerticalAlignment="Top" />
</Grid>
Which look like this...

Now, the Click Event for the CopyToClipboard Button would look like this...
private void CopyToClipboardButton_Click(object sender, RoutedEventArgs e)
{
string textToCopy = CopyTextTextBox.Text;
try
{
Clipboard.SetText(textToCopy);
}
catch (SecurityException se)
{
MessageBox.Show(se.Message);
}
}
and the Click Event for the PasteToTextBox Button would be this..
private void PasteToTextBoxButton_Click(object sender, RoutedEventArgs e)
{
string textFromClipboard = string.Empty;
try
{
textFromClipboard = Clipboard.GetText();
}
catch (SecurityException se)
{
MessageBox.Show(se.Message);
}
PasteTextTextBox.Text = textFromClipboard;
}
Now run the application and type into the Text To Copy textbox. When you click the Copy To Clipboard button, you will be prompted for permission for access to the clipboard.

After allowing access, click the Paste To TextBox button, and the text will now be in the other textbox.
Very simple tutorial for a very powerful feature.




MultiQuote




|