views:

209

answers:

1

I need to use a richtextbox, not a normal textbox because of the way it keeps the caret position, from line to line. But I need to keep the text at the same font all the time even if it is pasted.

At the moment I have it selecting the entire text and changing the font to the original (Lucida Console) but it look horrible when you paste into it as it flashes blue.

+2  A: 

If you are handling the pasting programatically don't use the Paste method. Instead use Clipboard.GetDataObject().GetData(DataFormats.Text) to get the text in a string and then add the text using the Rtf or Text property to the RichTextBox:

string s = (string)Clipboard.GetDataObject().GetData(DataFormats.Text);
richTextBox.Text += s;

Otherwise you could handle the Ctrl+V key press:

void RichTextBox1_KeyDown(object sender, KeyEventArgs e)
{
    if(e.Control == true && e.KeyCode == Keys.V)
    {
        string s = (string)Clipboard.GetDataObject().GetData(DataFormats.Text);
        richTextBox.Text += s;
        e.Handled = true; // disable Ctrl+V
    }
}
Darin Dimitrov
Thanks I changed it to vb.net, but there is one problem. doing `.text += s`Puts the text from the clipboard at the end of the textbox but if you are pasting then it needs to be at the place where the caret is.So the code is :`Dim s = Clipboard.GetDataObject.GetData(DataFormats.Text)``RichTextBox.Text.Insert(RichTextBox.SelectionStart, s)`
Jonathan