tags:

views:

531

answers:

3

In WPF, is there an easy to allow overwriting of text in an textbox?

Thanks
Tony

EDIT: I guess i wasn't to clear. Sorry.

I have a TextBox that the user is allowed to type 6 characters. If the user types 6 characters and then for whatever reason put the cursor at the start or somewhere in the middle of the 6 characters and starts to type, I want what they are typing to overwrite characters. Basically act like overwrite mode in Word.

Thanks again.

+1  A: 

Assuming you mean select some text and then allow the user to type over that text:

//select the third character
textBox.Select(2, 1);

HTH, Kent

Kent Boogaart
+2  A: 

Looking at it in Reflector, this seems to be controlled from the boolean TextBoxBase.TextEditor._OvertypeMode internal property. You can get at it through reflection:

// fetch TextEditor from myTextBox
PropertyInfo textEditorProperty = typeof(TextBox).GetProperty("TextEditor", BindingFlags.NonPublic | BindingFlags.Instance);
object textEditor = textEditorProperty.GetValue(myTextBox, null);

// set _OvertypeMode on the TextEditor
PropertyInfo overtypeModeProperty = textEditor.GetType().GetProperty("_OvertypeMode", BindingFlags.NonPublic | BindingFlags.Instance);
overtypeModeProperty.SetValue(textEditor, true, null);
Robert Macnee
A: 

I would avoid reflection. The cleanest solution is the following:

EditingCommands.ToggleInsert.Execute(null, myTextBox);

raf