Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 869235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:16:34+00:00 2026-05-15T10:16:34+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T10:16:35+00:00Added an answer on May 15, 2026 at 10:16 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to keep a track which pages are mostly accessed and heavy traffic.
Here is my problem, I want to track if user is online or offline
I want to track the last_login DateTime of my user, without changing the updated_at
I'm building a chatroom application, and I want to keep track of which users
I have a recursive algorithm which steps through a string, character by character, and
I want to track a laser light dot(which is on a wall) with a
I have a model which contains a ManyToMany to User to keep track of
I want to write a plug-in which tracks the updates to a text file
We want to track the time taken by a web service call between a
I want to track incoming traffic to a website from campaigns. A campain link

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.