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

  • Home
  • SEARCH
  • 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 8794487
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:15:29+00:00 2026-06-13T23:15:29+00:00

I have a TextBox that only allows certain characters to be typed into it;

  • 0

I have a TextBox that only allows certain characters to be typed into it; I’m handling this logic in the PreviewTextInput event. If it’s a character that’s allowed, then the TextChanged event is fired, otherwise it cancels the TextChanged event.

I have another application that is opened on a second screen while this is running, however even if there is keyboard input while the other application is active, it should still update the currently focused TextBox on the main application. To do this, I added a listener to the OnKeyPress() event on the second application, which calls the PreviewTextInput event on the focused TextBox in the main application.

Here is the code:

private void ImageEventController_OnKeyPress(char c)
        {
            object focusedElement = this.CurrentKeyboardFocus;
            if (focusedElement != null)
            {
                if (focusedElement is TextBox)
                {
                    TextBox target = (TextBox)focusedElement;
                    if (target.IsEnabled)
                    {
                        string text = c.ToString();
                        var routedEvent = TextCompositionManager.PreviewTextInputEvent;


         target.RaiseEvent(new TextCompositionEventArgs(
                    InputManager.Current.PrimaryKeyboardDevice,
                    new TextComposition(InputManager.Current, target, text)) { RoutedEvent = routedEvent });
                }
            }
        }
    }

When this is called, it goes through the PreviewTextInput event, however the TextChanged event never gets fired even if it’s a valid character. Is there any reason why TextChanged is not getting fired when PreviewTextInput is invoked programmatically?

UPDATE:

I added in this code to the bottom of my PreviewTextInput event listener:

if (!e.Handled)
{
     textbox.Text = e.Text;
}

This forces the TextChanged event to fire and fixes the functionality when the second application has focus, however if the main application has focus it causes two TextBoxes to get updated when only pressing one button.

  • 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-13T23:15:30+00:00Added an answer on June 13, 2026 at 11:15 pm

    I was unable to figure out how to invoke the TextChanged event from PreviewTextInput, but I did manage to accomplish what I needed to do:

    Instead of performing the logic to validate the key pressed is valid inside of the PreviewTextInput event, I pulled all of the logic out and put it into a public function. Then in my ImageEventController_OnKeyPress event I am using the LogicalTreeHelper.GetParent() method to find the necessary control. From there I call the public function to validate a valid key is pressed and if it is, I call the TextInput directly.

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

Sidebar

Related Questions

I have a textbox that accepts only these values: . , - $ m
I have a textbox that I would like for only numbers. But if I
I have used auto complete textbox previously. That auto complete works only when i
I have a textbox that when you put focus into it, it drops down
I have a TextBox that has the TextChanged event set declaratively. In some cases,
Here's the scenario. I have a search page with a TextBox that allows someone
I have a query builder form that allows a user to enter values into
all. I have a usercontrol NumericTextBox that only allows numeric entries. I need to
I have a textbox that the user can input into. Right now the user
I have a textbox that holds From month. (allows values between 1 and 12

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.