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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T16:12:07+00:00 2026-05-13T16:12:07+00:00

Question: Is there a good way in Silverlight to intercept undesirable characters from being

  • 0

Question:

Is there a good way in Silverlight to intercept undesirable characters from being entered into a textbox?

Background:

I have a textbox which allows a user to enter a filename. I would like to exclude invalid file characters from being entered into the textbox. A few of these characters are:

  • ‘?’
  • ‘\’
  • ‘<‘
  • ‘>’

Although the Silverlight TextBox class does not support a KeyPress event, it does have a KeyDown and a KeyUp event that can be used to retrieve character information when a key is entered into the textbox. It exposes these as a member of the Key enumeration or it can return an int for the PlatformKeyCode.

Of course the range of keys is larger/different from the range of characters – “F keys” are an example of this. However the presence of something like a KeyPress event in Windows Forms is indicative of the usefulness of being able to extract specific character information.

To do a proof of concept that things could work I hardcoded the PlatformKeyCode values for the undesired characters for my platform into the event handler and everything worked… but of course this is just my platform. I need to make sure this implementation is platform agnostic. Here is the code to demonstrate how I would like it to work:

    private void theText_KeyDown(object sender, KeyEventArgs e)
    {
        int[] illegals = { 191, 188, 190, 220, 186, 222, 191, 56, 186};
        if (illegals.Any(i => i == e.PlatformKeyCode)) e.Handled = true;
    }
  • 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-13T16:12:07+00:00Added an answer on May 13, 2026 at 4:12 pm

    Both responses (in comments) from Henrik and Johannes contain the best answer for the scenario. Rather than thinking like a Windows Forms programmer and capturing the specific key event, the canonical approach in Silverlight is to use the TextChanged event to remove undesirable characters from a TextBox. A similar question on allowing only numeric input to a TextBox is what prompted the solution that was used.

    The following code samples the approach I took after reading the comments and it works quite well to remove characters inappropriate for entry:

        private void fileNameTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {
            string illegalChars = @"?<>:""\/*|";
            fileNameTextBox.Text = String.Join("", fileNameTextBox.Text.Split(illegalChars.ToCharArray()));
            fileNameTextBox.SelectionStart = fileNameTextBox.Text.Length;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Question Is there a way to have a method that will always run anytime
As the question states - is there a good tool out there which lists
Following this question: Good crash reporting library in c# Is there any library like
I'm sure this is a relatively simple question, and there must be a good
There are many skills a programmer could have (understanding the problem, asking good questions,
Question: Is there an easy way (library function) to perform a bitwise AND or
We have the question is there a performance difference between i++ and ++i in
I have seen this question: Are there any decent UI components for touch screen
Or to reformulate the question: is there a performance penalty in using unsigned values?
Since I've gotten no answer at all to my question Is there an alternative

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.