question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get TextBox to only accept numbers?

See original GitHub issue

I looked around for ages but couldn’t find anything on this. I tried setting up validation (which is also very undocumented), but still have issues with that.

My TextBox is supposed to only accept unsigned integers. I think there should be some easy property to set to TextBox to define what types of inputs it can accept. (Android has this)

I tried

public uint? AppId {
get => _appId;
set
  {
    if(!(value is uint?))
    {
      throw new DataValidationException("Invalid Steam App Id");
    }

    _appId = value;
  }
}

which I got from #4845, but the problem is that the program still closes with System.ArgumentException: 'f is not a valid value for UInt32. (Parameter 'value')', since the property itself doesn’t accept strings.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
maxkatz6commented, Oct 22, 2020

@SpyceTewan as mentioned above, you can use Converter or Behaviors.

Although I would be nice to have UWP InputScope alternative (possible values - link). With that this issue could be solved simply <TextBox InputScrope="Number" /> But I believe this feature should be planned and introduced only with IME and mobile in mind @kekekeks.

1reaction
Dorin-Foaltincommented, Jun 14, 2021

@StefanH-AT: have you managed to fully restrict input of invalid characters, like letters (eg: ‘a’ or ‘b’)?

I’m trying to allow only entering of numbers, and it seems like this should work, but for me it only works after the app is minimized + restored, forcing a redraw.

I’ve tried calling InvalidateVisual()/ other methods after KeyDown/ KeyUp/ ValueChanghed, but this doesn’t really fix it, so I was thinking maybe you have a work around for it?

In this gif below, see that I can enter letters, these are not cleared until I minimize and restore the app:

NumericUpDownRefreshIssue

Read more comments on GitHub >

github_iconTop Results From Across the Web

How do I make a textbox that only accepts numbers?
Simply adding something like myNumbericTextBox.RegexString = "^(\\d+|)$"; should suffice. Share. Share a ...
Read more >
Help, how to Make textbox accept numbers only windows ...
It can only accept the input of numbers 0-9, backspace and decimal point. 8 stands for backspace, 48 stands for number 0, 57...
Read more >
How to force Input field to enter numbers only using ...
Now forcing input field type=”text” to accept numeric values only by using Javascript or jQuery. You can also set type=”tel” attribute in the ......
Read more >
How to allow only numbers inside a textbox in Winforms C# ...
If you want to create an input that only accepts number, the first thing that you need to think in is the NumericUpDown...
Read more >
text box to only accept numbers
4. In case anyone is looking for a solution to the above question: you can use html/javascript snippet and add a class to...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found