I am using Rich Text object in my C# application. The only issue I am having is that when user pastes formated text from another app, it remains formated which I cannot have. Is there any way how to paste only string and ignore formatting? Thanks!
相关问题
- Sorting 3 numbers without branching [closed]
- Graphics.DrawImage() - Throws out of memory except
- Why am I getting UnauthorizedAccessException on th
- 求获取指定qq 资料的方法
- How to know full paths to DLL's from .csproj f
Assuming WinForms : try this : define a RichTextBox with a KeyDown event handler like this :
Append-only example :
[Edit]
Add Clipboard RTF to RichTextBox at current insertion point (selection start) example :
[End Edit]
Note 0 : The pasted in text is going to assume the current style settings in effect for the RichTextBox : if you have 'ForeGround color set to 'Blue : the pasted in text is going to be in blue.
Note 1 : This is something I knocked together quickly, and tested only a few times by creating some multi-colored and weirdly formatted RTF for the clipboard using WordPad : then pasting into into the RichTextBox1 at run-time : it did strip away all the color, indenting, etc.
Since it's not fully tested, use caution.
Note 2 : This will not handle the case of 'Insert or 'Paste via Context Menu, obviously.
Welcome all critiques of this answer, and will immediately take it down if it's not "on the mark."
I was searching for a plaintext-only
richtextbox
but haven't found the solution online.Why Plaintext-only
RichTextBox
instead of aTextBox
? For example becauseRichTextBox
has usable undo/redo functionality and much more.Finally I found a perfect solution by digging into the C header files of the richedit control: A
RichTextBox
can be switched into plaintext mode, after that it doesn't accept formatted text and images and similar things from the clipboard and behaves like a normalTextBox
formattingwise. Fancy things like images can not be pasted and it pastes formatted text by removing the formatting.Well the RichTextBox has a
SelectionFont
property so you can for instance do the following:If a text gets pasted, it will be automatically formatted.
Add a handler to the
KeyDown
-event to intercept the standard paste and manually insert only the plain text: