Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to track which character is deleted in TextBox in WPF?

I want to track which character is deleted by the user through Delete or BackSpace Key.

I am handling TextBox_ChangedEvent of textbox.

Can I extract the deleted character from TextChangedEventArgs e.Changes and if yes How can I do that?

I want to restrict user to from deleting any characters from the TextBox. I want user can delete only two characters ( let's say "(" or ")" )

Please suggest.

like image 452
Ashish Ashu Avatar asked Jun 16 '10 08:06

Ashish Ashu


2 Answers

Below you will find code for an attached property that can be used like this to prevent anything but "(" or ")" from being deleted from the TextBox, period.

<TextBox my:TextBoxRestriction.RestrictDeleteTo="()" ... />

This will correctly handle all mouse and keyboard updates, such as:

  1. Use of the Delete key with multiple characters selected
  2. Use of the Backspace key
  3. Use of Ctrl-X to cut
  4. Clicking the "Cut" button on your menu bar

Because of this it is much more powerful than simply intercepting PreviewKeyDown.

This also disables deletion of anything byt "(" or ")" by assigning directly to the .Text property, so this will fail:

textBox.Text = "Good morning";

Because of this the TextBoxRestriction class also contains another attached property called UnrestrictedText which, when set, is able to update the Text property bypassing the restrictions. This can be set in code using TextBoxRestriction.SetUnrestrictedText, or data-bound like this:

<TextBox my:TextBoxRestriction.RestrictDeleteTo="()"
         my:TextBoxRestriction.UnrestrictedText="{Binding PropertyNameHere}" />

In the implementation below, UnrestrictedText only works when RestrictDeleteTo is also set. A full implementation could be made that registers the event handler whenever either property is set and saves the handler in a third attached property for later unregistration. But for your current needs that is probably unnecessary.

Here is the implementation as promised:

public class TextBoxRestriction : DependencyObject
{
  // RestrictDeleteTo:  Set this to the characters that may be deleted
  public static string GetRestrictDeleteTo(DependencyObject obj) { return (string)obj.GetValue(RestrictDeleteToProperty); }
  public static void SetRestrictDeleteTo(DependencyObject obj, string value) { obj.SetValue(RestrictDeleteToProperty, value); }
  public static readonly DependencyProperty RestrictDeleteToProperty = DependencyProperty.RegisterAttached("RestrictDeleteTo", typeof(string), typeof(TextBoxRestriction), new PropertyMetadata
  {
    PropertyChangedCallback = (obj, e) =>
      {
        var box = (TextBox)obj;
        box.TextChanged += (obj2, changeEvent) =>
          {
            var oldText = GetUnrestrictedText(box);
            var allowedChars = GetRestrictDeleteTo(box);
            if(box.Text==oldText || allowdChars==null) return;

            foreach(var change in changeEvent.Changes)
              if(change.RemovedLength>0)
              {
                string deleted = box.Text.Substring(change.Offset, change.RemovedLength);
                if(deleted.Any(ch => !allowedChars.Contains(ch)))
                  box.Text = oldText;
              }
            SetUnrestrictedText(box, box.Text);
          };
      }
  });

  // UnrestrictedText:  Bind or access this property to update the Text property bypassing all restrictions
  public static string GetUnrestrictedText(DependencyObject obj) { return (string)obj.GetValue(UnrestrictedTextProperty); }
  public static void SetUnrestrictedText(DependencyObject obj, string value) { obj.SetValue(UnrestrictedTextProperty, value); }
  public static readonly DependencyProperty UnrestrictedTextProperty = DependencyProperty.RegisterAttached("UnrestrictedText", typeof(string), typeof(TextBoxRestriction), new PropertyMetadata
  {
    DefaultValue = "",
    PropertyChangedCallback = (obj, e) =>
      {
        var box = (TextBox)obj;
        box.Text = (string)e.NewValue;
      }
  });

}

How it works: When you set UnrestrictedText it sets Text and vice versa. The TextChanged handler checks to see if Text is different than UnrestrictedText. If so, it knows that Text has been updated by some other mechanism than setting UnrestrictedText so is scans the changes for an illegal delete. If one is found it sets Text back to the value still stored in UnrestrictedText, preventing the change.

like image 149
Ray Burns Avatar answered Nov 12 '22 00:11

Ray Burns


An attached behaviour to handle it

public static class TextInputBehaviour
{
    public static bool GetIsDeleteRestricted(DependencyObject obj)
    {
        return (bool)obj.GetValue(IsDeleteRestrictedProperty);
    }

    public static void SetIsDeleteRestricted(DependencyObject obj, bool value)
    {
        obj.SetValue(IsDeleteRestrictedProperty, value);
    }

    public static readonly DependencyProperty IsDeleteRestrictedProperty=DependencyProperty.RegisterAttached("IsDeleteRestricted", typeof(bool), typeof(TextInputBehaviour), new UIPropertyMetadata(false, OnIsDeleteRestrictedChanged));
}

private static void OnIsDeleteRestrictedChanged(object sender, DependencyPropertyChangedEventArgs e)
{      
  TextBox textBox = (TextBox)sender;
  bool isDeleteRestricted = (bool)(e.NewValue);

  if (isDeleteRestricted)
    textBox.PreviewKeyDown += RestrictDeleteKey;
  else
    textBox.PreviewKeyDown -= RestrictDeleteKey;
}

private static void RestrictDeleteKey(object sender, KeyEventArgs e)
{
      e.Handled = (e.Key == Key.Delete);
}

Drop the behaviour in the resources section

Then in your textbox markup block, set the behaviour

<TextBox local:TextInputBehaviour.IsDeleteRestricted="True" />
like image 41
Amsakanna Avatar answered Nov 12 '22 00:11

Amsakanna