Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to prevent or override automatic formatting within RichTextBox

How would i prevent text from being automatically formatted when added to a RichTextBox, or better yet, override the formatting that does occur?

For instance, the following code sets the text, but then creates a formatted link to a server.

rtbSomeField.Text = "\\\\just some text";

Results in

alt text

I understand why it's doing this, but is there a way to disable or override that particular feature?

like image 958
b w Avatar asked Oct 19 '25 15:10

b w


2 Answers

Set DetectUrls to false for the RichTextBox

Good luck!

like image 133
Homam Avatar answered Oct 21 '25 03:10

Homam


You can use the following method to paste text into RichTextBox as plain text. First, you need to add pasting handler to RichTextBox:

System.Windows.DataObject.AddPastingHandler(rtbox, ClearClipboardFormat.OnPaste);

Second - change desired format in handler:

static class ClearClipboardFormat
{        
    public static void OnPaste(object sender, DataObjectPastingEventArgs e)
    {
        e.FormatToApply = DataFormats.Text;            
    }
}
like image 32
Bounz Avatar answered Oct 21 '25 05:10

Bounz