C # RTB - insert plain text without colors / fonts?

I am using a Rich Text object in my C # application. The only problem I am facing is that when the user inserts the generated text from another application, it remains formed, which I cannot. Is there a way to insert only a string and ignore formatting? Thanks!

+4
source share
8 answers

Assuming WinForms: try this: define a RichTextBox with a KeyDown event handler as follows:

Example to add only:

private void richTextBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.V) { richTextBox1.Text += (string)Clipboard.GetData("Text"); e.Handled = true; } } 

[Change]

Add RTF clipboard to RichTextBox at current insertion point (selection example):

 private void richTextBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.V) { // suspend layout to avoid blinking richTextBox2.SuspendLayout(); // get insertion point int insPt = richTextBox2.SelectionStart; // preserve text from after insertion pont to end of RTF content string postRTFContent = richTextBox2.Text.Substring(insPt); // remove the content after the insertion point richTextBox2.Text = richTextBox2.Text.Substring(0, insPt); // add the clipboard content and then the preserved postRTF content richTextBox2.Text += (string)Clipboard.GetData("Text") + postRTFContent; // adjust the insertion point to just after the inserted text richTextBox2.SelectionStart = richTextBox2.TextLength - postRTFContent.Length; // restore layout richTextBox2.ResumeLayout(); // cancel the paste e.Handled = true; } } 

[Editing the end]

Note 0: the nested text will accept the current style settings for the RichTextBox: if you have the ForeGround color set to β€œBlue”: the nested text will be blue.

Note 1: This is what I quickly knocked down and checked only a few times, creating several multi-colored and strangely formatted RTFs for the clipboard using WordPad: then pasting into RichTextBox1 at runtime: open all color, indentation, etc.

Since it is not fully tested, use caution.

Note 2: This will not handle the β€œPaste” or β€œPaste through the context menu” case, obviously.

We welcome all critical comments on this issue and immediately remove it if it is not "by sign".

+7
source

Add a handler to KeyDown -event to intercept the standard paste and manually insert only plain text:

 private void rtb_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.V) { ((RichTextBox)sender).Paste(DataFormats.GetFormat("Text")); e.Handled = true; } } 
+22
source

I was only looking for richtextbox text, but did not find a solution on the Internet.

Why is Plaintext-only richtextbox instead of TextBox ? For example, because richtextbox has undo / redo functionality and more.

Finally, I found the perfect solution by digging into the C header files of the richedit: A richtextbox control, you can switch to plain text mode, after which it does not accept formatted text and images and similar things from the clipboard and behaves like normal TextBox formatting. Unusual things, such as images, cannot insert or paste formatted text, removing formatting.

 class PlainRichTextBox : RichTextBox { const int WM_USER = 0x400; const int EM_SETTEXTMODE = WM_USER + 89; const int EM_GETTEXTMODE = WM_USER + 90; // EM_SETTEXTMODE/EM_GETTEXTMODE flags const int TM_PLAINTEXT = 1; const int TM_RICHTEXT = 2; // Default behavior const int TM_SINGLELEVELUNDO = 4; const int TM_MULTILEVELUNDO = 8; // Default behavior const int TM_SINGLECODEPAGE = 16; const int TM_MULTICODEPAGE = 32; // Default behavior [DllImport("user32.dll")] static extern IntPtr SendMessage(IntPtr hWnd, int wMsg, IntPtr wParam, IntPtr lParam); bool m_PlainTextMode; // If this property doesn't work for you from the designer for some reason // (for example framework version...) then set this property from outside // the designer then uncomment the Browsable and DesignerSerializationVisibility // attributes and set the Property from your component initializer code // that runs after the designer code. [DefaultValue(false)] //[Browsable(false)] //[DesignerSerializationVisibility(DesignerSerializationVisibility.Hidden)] public bool PlainTextMode { get { return m_PlainTextMode; } set { m_PlainTextMode = value; if (IsHandleCreated) { IntPtr mode = value ? (IntPtr)TM_PLAINTEXT : (IntPtr)TM_RICHTEXT; SendMessage(Handle, EM_SETTEXTMODE, mode, IntPtr.Zero); } } } protected override void OnHandleCreated(EventArgs e) { // For some reason it worked for me only if I manipulated the created // handle before calling the base method. PlainTextMode = m_PlainTextMode; base.OnHandleCreated(e); } } 
+4
source

The answer from pasztorpisti worked like a charm for me. Since I use vb.net, I thought I would post the translated code for others:

 Imports System.Runtime.InteropServices Imports System.ComponentModel Public Class MyRichTextBox Inherits Windows.Forms.RichTextBox Public Const WM_USER As Integer = &H400 Public Const EM_SETTEXTMODE As Integer = WM_USER + 89 Public Const EM_GETTEXTMODE As Integer = WM_USER + 90 'EM_SETTEXTMODE/EM_GETTEXTMODE flags Public Const TM_PLAINTEXT As Integer = 1 Public Const TM_RICHTEXT As Integer = 2 ' Default behavior Public Const TM_SINGLELEVELUNDO As Integer = 4 Public Const TM_MULTILEVELUNDO As Integer = 8 ' Default behavior Public Const TM_SINGLECODEPAGE As Integer = 16 Public Const TM_MULTICODEPAGE As Integer = 32 ' Default behavior <DllImport("user32.dll", SetLastError:=True, CharSet:=CharSet.Auto)> _ Private Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal Msg As UInteger, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As IntPtr End Function Private _plainTextMode As Boolean = False <DefaultValue(False), Browsable(True)> Public Property PlainTextMode As Boolean Get Return _plainTextMode End Get Set(value As Boolean) _plainTextMode = value If (Me.IsHandleCreated) Then Dim mode As IntPtr = If(value, TM_PLAINTEXT, TM_RICHTEXT) SendMessage(Handle, EM_SETTEXTMODE, mode, IntPtr.Zero) End If End Set End Property Protected Overrides Sub OnHandleCreated(e As EventArgs) 'For some reason it worked for me only if I manipulated the created 'handle before calling the base method. Me.PlainTextMode = _plainTextMode MyBase.OnHandleCreated(e) End Sub End Class 
+1
source

Well, a RichTextBox has a SelectionFont property so that you can, for example, do the following:

 Font courier; courier = new Font("Courier new", 10f, FontStyle.Regular); myRtb.SelectionFont = courier; myRtb.Font = courier; //So the typed text is also the same font 

If the text is inserted, it will be automatically formatted.

0
source

You can also use

 private void richTextBox1_KeyDown(object sender, KeyEventArgs e) { if (e.Control && e.KeyCode == Keys.V) { richTextBox1.SelectedText = (string)Clipboard.GetData("Text"); e.Handled = true; } } 
0
source

Simple, but everything in the clipboard is in clear text when the application is open.

 private void timer2_Tick(object sender, EventArgs e) { string paste = Clipboard.GetText(); Clipboard.SetText(paste); } 
0
source

I achieved this by omitting the font and color for the entire RTB when its contents changed. This works fine for me as there is no need to process a huge amount of text in the input field.

 public FormMain() { InitializeComponent(); txtRtb.TextChanged += txtRtb_TextChanged; } void txtRtb_TextChanged(object sender, EventArgs e) { RichTextBox rtb = (RichTextBox)sender; rtb.SelectAll(); rtb.SelectionFont = rtb.Font; rtb.SelectionColor = System.Drawing.SystemColors.WindowText; rtb.Select(rtb.TextLength,0); } 
0
source

All Articles