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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T13:14:23+00:00 2026-05-12T13:14:23+00:00

I am currently working on a WPF application where I would like to have

  • 0

I am currently working on a WPF application where I would like to have a TextBox that can only have numeric entries in it. I know that I can validate the content of it when I lost the focus and block the content from being numeric, but in other Windows Form application, we use to totally block any input except numerical from being written down. Plus, we use to put that code in a separate dll to reference it in many places.

Here is the code in 2008 not using WPF:

Public Shared Sub BloquerInt(ByRef e As System.Windows.Forms.KeyPressEventArgs, ByRef oTxt As Windows.Forms.TextBox, ByVal intlongueur As Integer)
    Dim intLongueurSelect As Integer = oTxt.SelectionLength
    Dim intPosCurseur As Integer = oTxt.SelectionStart
    Dim strValeurTxtBox As String = oTxt.Text.Substring(0, intPosCurseur) & oTxt.Text.Substring(intPosCurseur + intLongueurSelect, oTxt.Text.Length - intPosCurseur - intLongueurSelect)

    If IsNumeric(e.KeyChar) OrElse _
       Microsoft.VisualBasic.Asc(e.KeyChar) = System.Windows.Forms.Keys.Back Then
        If Microsoft.VisualBasic.AscW(e.KeyChar) = System.Windows.Forms.Keys.Back Then
            e.Handled = False
        ElseIf strValeurTxtBox.Length < intlongueur Then
            e.Handled = False
        Else
            e.Handled = True

        End If
    Else
        e.Handled = True
    End If

Is there an equivalent way in WPF? I wouldn’t mind if this is in a style but I am new to WPF so style are a bit obscure to what they can or can’t do.

  • 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-12T13:14:23+00:00Added an answer on May 12, 2026 at 1:14 pm

    You can restrict the input to numbers only using an attached property on the TextBox. Define the attached property once (even in a separate dll) and use it on any TextBox. Here is the attached property:

       using System;
       using System.Windows;
       using System.Windows.Controls;
       using System.Windows.Input;
    
       /// <summary>
       /// Class that provides the TextBox attached property
       /// </summary>
       public static class TextBoxService
       {
          /// <summary>
          /// TextBox Attached Dependency Property
          /// </summary>
          public static readonly DependencyProperty IsNumericOnlyProperty = DependencyProperty.RegisterAttached(
             "IsNumericOnly",
             typeof(bool),
             typeof(TextBoxService),
             new UIPropertyMetadata(false, OnIsNumericOnlyChanged));
    
          /// <summary>
          /// Gets the IsNumericOnly property.  This dependency property indicates the text box only allows numeric or not.
          /// </summary>
          /// <param name="d"><see cref="DependencyObject"/> to get the property from</param>
          /// <returns>The value of the StatusBarContent property</returns>
          public static bool GetIsNumericOnly(DependencyObject d)
          {
             return (bool)d.GetValue(IsNumericOnlyProperty);
          }
    
          /// <summary>
          /// Sets the IsNumericOnly property.  This dependency property indicates the text box only allows numeric or not.
          /// </summary>
          /// <param name="d"><see cref="DependencyObject"/> to set the property on</param>
          /// <param name="value">value of the property</param>
          public static void SetIsNumericOnly(DependencyObject d, bool value)
          {
             d.SetValue(IsNumericOnlyProperty, value);
          }
    
          /// <summary>
          /// Handles changes to the IsNumericOnly property.
          /// </summary>
          /// <param name="d"><see cref="DependencyObject"/> that fired the event</param>
          /// <param name="e">A <see cref="DependencyPropertyChangedEventArgs"/> that contains the event data.</param>
          private static void OnIsNumericOnlyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
          {
             bool isNumericOnly = (bool)e.NewValue;
    
             TextBox textBox = (TextBox)d;
    
             if (isNumericOnly)
             {
                textBox.PreviewTextInput += BlockNonDigitCharacters;
                textBox.PreviewKeyDown += ReviewKeyDown;
             }
             else
             {
                textBox.PreviewTextInput -= BlockNonDigitCharacters;
                textBox.PreviewKeyDown -= ReviewKeyDown;
             }
          }
    
          /// <summary>
          /// Disallows non-digit character.
          /// </summary>
          /// <param name="sender">The source of the event.</param>
          /// <param name="e">An <see cref="TextCompositionEventArgs"/> that contains the event data.</param>
          private static void BlockNonDigitCharacters(object sender, TextCompositionEventArgs e)
          {
             foreach (char ch in e.Text)
             {
                if (!Char.IsDigit(ch))
                {
                   e.Handled = true;
                }
             }
          }
    
          /// <summary>
          /// Disallows a space key.
          /// </summary>
          /// <param name="sender">The source of the event.</param>
          /// <param name="e">An <see cref="KeyEventArgs"/> that contains the event data.</param>
          private static void ReviewKeyDown(object sender, KeyEventArgs e)
          {
             if (e.Key == Key.Space)
             {
                // Disallow the space key, which doesn't raise a PreviewTextInput event.
                e.Handled = true;
             }
          }
       }
    

    Here is how to use it (replace “controls” with your own namespace):

    <TextBox controls:TextBoxService.IsNumericOnly="True" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 257k
  • Answers 257k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Because LocationType is the superclass; it can't be cast to… May 13, 2026 at 10:50 am
  • Editorial Team
    Editorial Team added an answer Could not locate the assembly "WPFToolkit, Version=3.5.40128.1, Culture=neutral, PublicKeyToken=31bf3856ad364e35". WPFToolkit… May 13, 2026 at 10:50 am
  • Editorial Team
    Editorial Team added an answer stores multiple versions in the same svn repo but in… May 13, 2026 at 10:50 am

Related Questions

I am currently working with the Microsoft MVVM template and find the lack of
I've been working on a Composite WPF application and just read some good guidelines
I am currently working on a project of mine which is a MSPaint-like WPF
I am currently working on a small N-Tier application in C# which uses Linq-to-Entities

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.