Reputation: 27
My assignment program has Cut, Copy and Paste methods for rich text in a richtextbox. However, when I Copy and then Paste, I'm getting extra characters and numbers pasted and I don't know where they're coming from.
Are my Cut, Copy and Paste methods incorrect? I have done a lot of searching, but I can't seem to pinpoint the issue. My test file has the words "test" or "Testing" in a few lines, but when I Paste, I get things like "ng1033" pasted in.
private void cutText()
{
Clipboard.Clear();
if(desktopRichText.SelectionLength > 0)
{
//cut selected text to clipboard
Clipboard.SetText(desktopRichText.SelectedRtf);
desktopRichText.SelectedRtf = string.Empty;
}
else
{
MessageBox.Show("Please select text to modify");
}
}
private void copyText()
{
Clipboard.Clear();
if(desktopRichText.SelectionLength > 0)
{
//copies selected text to clipboard
Clipboard.SetText(desktopRichText.SelectedRtf);
}
else
{
MessageBox.Show("Please select text to modify");
}
}
private void pasteText()
{
if (Clipboard.ContainsText())
{
//pastes text on clipboard to richtextbox
string cutText = Clipboard.GetText();
desktopRichText.SelectedRtf = desktopRichText.SelectedRtf.Insert(desktopRichText.SelectionStart, cutText);
}
else
{
MessageBox.Show("Please select text to modify");
}
}
Upvotes: 1
Views: 222
Reputation: 125197
To do copy, cut or paste in RichTextBox
, use the corresponding methods of the control:
Example
private void CopyButton_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength > 0)
richTextBox1.Copy();
}
private void CutButton_Click(object sender, EventArgs e)
{
if (richTextBox1.SelectionLength > 0)
richTextBox1.Cut();
}
private void PasteButton_Click(object sender, EventArgs e)
{
if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
richTextBox1.Paste();
}
Upvotes: 1