How can I insert text into a WPF textbox at caret position? What am I missing? In Win32 you could use CEdit::ReplaceSel().
It should work as if the Paste() command was invoked. But I want to avoid using the clipboard.
To simply insert text at the caret position:
textBox.Text = textBox.Text.Insert(textBox.CaretIndex, "<new text>");
To replace the selected text with new text:
textBox.SelectedText = "<new text>";
To scroll the textbox to the caret position:
int lineIndex = textBox.GetLineIndexFromCharacterIndex(textBox.CaretIndex); textBox.ScrollToLine(lineIndex);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With