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 8112905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T02:39:38+00:00 2026-06-06T02:39:38+00:00

When using textbox.Undo(); I get the following error: Cannot Undo or Redo while undo

  • 0

When using textbox.Undo(); I get the following error:

Cannot Undo or Redo while undo unit is open.

Now I understand why this is the case (because it is an active undoable event) but through what event can I perform validation on a text box and undo the change if the user has typed an invalid character?

  • 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-06-06T02:39:39+00:00Added an answer on June 6, 2026 at 2:39 am

    Instead of using Undo and TextChanged, you should use the PreviewTextInput and DataObject.Pasting events. In the PreviewTextInput event handler, set e.Handled to true if the typed text is an invalid character. In the Pasting event handler, call e.CancelCommand() if the pasted text is invalid.

    Here is an example for a text box that accepts only the digits 0 and 1:

    XAML:

    <Window x:Class="BinaryTextBox.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            Title="MainWindow" Height="133" Width="329">
        <StackPanel>
            <TextBox x:Name="txtBinary" Width="100" Height="24"
                     PreviewTextInput="txtBinary_PreviewTextInput"
                     DataObject.Pasting="txtBinary_Pasting"/>
        </StackPanel>
    </Window>
    

    Code behind:

    using System.Text.RegularExpressions;
    using System.Windows;
    using System.Windows.Input;
    
    namespace BinaryTextBox
    {
        /// <summary>
        /// Interaction logic for MainWindow.xaml
        /// </summary>
        public partial class MainWindow : Window
        {
            public MainWindow()
            {
                InitializeComponent();
            }
    
            private void txtBinary_PreviewTextInput(object sender,
                     TextCompositionEventArgs e)
            {
                e.Handled = e.Text != "0" && e.Text != "1";
            }
    
            private void txtBinary_Pasting(object sender, DataObjectPastingEventArgs e)
            {
                if (!Regex.IsMatch(e.DataObject.GetData(typeof(string)).ToString(), "^[01]+$"))
                {
                    e.CancelCommand();
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to validate my textbox using JavaScript, so far i made this :
I'm trying to implement autocomplete for a textbox using the following code, but it's
How can I get the length of text entered in a textbox using jQuery?
I am trying to get Undo/Redo keyboard shortcuts working in my WPF application (I
I currently have a TextBox using: <%: Html.TextBox(TextBox1) %> How do I get the
I was using a textbox to do the following: <input style=width:50px; type=text id=blah value=@model.value1
This is my code to fill a TextBox using the SendMessage function: C :=
I am trying to create a multiline Textbox using ASP.NET MVC with the following
I am using a multiline textbox, and I am getting behavior I cannot fully
I'm writing a multiple undo/redo system for a custom textbox control in Silverlight. The

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.