From 7bbaa54ea658d429e54234cde64b0c3df17b26d7 Mon Sep 17 00:00:00 2001 From: Jonas Arnold Date: Thu, 4 May 2023 11:24:05 +0200 Subject: [PATCH] fixed paste into RichTextBox in MultiFormatTextBox --- .../MultiFormatTextBox/MultiFormatTextBox.cs | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs b/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs index 8c97f12..626ea8f 100644 --- a/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs +++ b/MultiTerm.Wpf.CustomControl/MultiFormatTextBox/MultiFormatTextBox.cs @@ -114,6 +114,7 @@ public class MultiFormatTextBox : Control this.richTextBox.PreviewKeyDown += RichTextBox_KeyDown; this.richTextBox.PreviewTextInput += RichTextBox_PreviewTextInput; this.richTextBox.SelectionChanged += RichTextBox_SelectionChanged; + DataObject.AddPastingHandler(this.richTextBox, OnRtbPaste); } else { @@ -352,6 +353,24 @@ public class MultiFormatTextBox : Control this.InsertCharacterAtCaretPositionIntoCurrentMultiFormatString(e.Text); } + private void OnRtbPaste(object sender, DataObjectPastingEventArgs e) + { + // get pasted data + if (e.DataObject.GetDataPresent(typeof(string))) + { + // get data as string + var text = (string)e.DataObject.GetData(typeof(string)); + // insert each character seperately + foreach (var character in text) + { + this.InsertCharacterAtCaretPositionIntoCurrentMultiFormatString(character.ToString()); + } + } + + // cancel pasting + e.CancelCommand(); + } + private void InsertCharacterAtCaretPositionIntoCurrentMultiFormatString(string text) { var caretPosition = this.GetRtbCaretPosition();