Tutorial :WPF TextBox Overwrite



Question:

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.


Solution:1

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);  


Solution:2

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

EditingCommands.ToggleInsert.Execute(null, myTextBox);


Solution:3

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

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


Solution:4

Heyho, i know this question is super old, but i was looking for an solution to archive the "override" behaviour via MVVM pattern. So i wrote the following DependencyProperty, hope it will help someone.

public class ElementBehavior  {      #region AlwaysOverride        ///<summary>      /// DependencyProperty      ///</summary>      public static readonly DependencyProperty AlwaysOverrideProperty = DependencyProperty.RegisterAttached("AlwaysOverride", typeof(bool), typeof(ElementBehavior), new PropertyMetadata(false, OnAlwaysOverrideChanged));        ///<summary>      /// Get      ///</summary>      ///<param name="target">DependencyObject</param>      ///<returns>ICommand</returns>      public static bool GetAlwaysOverride(DependencyObject target)      {          return (bool)target.GetValue(AlwaysOverrideProperty);      }        ///<summary>      /// Set      ///</summary>      ///<param name="target">DependencyObject</param>      ///<param name="value">ICommand</param>      public static void SetAlwaysOverride(DependencyObject target, bool value)      {          target.SetValue(AlwaysOverrideProperty, value);      }        ///<summary>1      /// OnChanged      ///</summary>      ///<param name="target">DependencyObject</param>      ///<param name="e">DependencyPropertyChangedEventArgs</param>      public static void OnAlwaysOverrideChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)      {          if (target.GetType() == typeof(TextBox))          {              TextBox t = (TextBox)target;              if ((bool)e.NewValue)              {                  t.PreviewKeyDown += OnAlwaysOverride;              }              else              {                  t.PreviewKeyDown -= OnAlwaysOverride;              }          }      }        private static void OnAlwaysOverride(object sender, KeyEventArgs e)      {          TextBox t = (TextBox)e.Source;          Key[] BAD_KEYS = new Key[] { Key.Back, Key.Delete };          Key[] WRK_KEYS = new Key[] { Key.Left, Key.Up, Key.Right, Key.Down, Key.Enter };          if (BAD_KEYS.Contains(e.Key))          {              e.Handled = true;          }          else if (WRK_KEYS.Contains(e.Key) == false && string.IsNullOrEmpty(t.Text) == false)          {              t.Select(t.CaretIndex, 1);          }      }      





        
Previous
Next Post »