-
Oct 13th, 2021, 12:53 AM
#1
Thread Starter
Hyperactive Member
Why NumericUpDown control allows more than one decimal separators?
Hello!
I'm working with a NumericUpDown control, that must receive decimal numbers, All my operations perform OK, but Why this NumericUpDown control allows me to input more than one point (decimal separator)?
I just need one.
I have tried this, and no success:
vb.net Code:
Dim DecimalSeparator As String = Application.CurrentCulture.NumberFormat.NumberDecimalSeparator
e.Handled = Not (Char.IsDigit(e.KeyChar) Or Asc(e.KeyChar) = 8 Or (e.KeyChar = DecimalSeparator And DirectCast(sender, TextBox).Text.IndexOf(DecimalSeparator) = -1))
Any help will be appreciated.
-
Oct 13th, 2021, 01:31 AM
#2
Re: Why NumericUpDown control allows more than one decimal separators?
Firstly, any input that doesn't represent a valid number is rejected when the control loses focus and it reverts to its previous value.
Secondly, while I don't know this for a fact, I would presume that Microsoft did this because validation on a per-key-press basis is complex. It's very easy to say that a character should be rejected if it is not a digit, separator or sign but it is more complex to say that a particular character should be allowed in some situations but not others. If you were to specifically prevent a second dot then the user could not enter a second dot and then delete the first, which might be natural to some people, but would be forced to delete the first dot before adding the second.
-
Oct 13th, 2021, 10:00 AM
#3
Re: Why NumericUpDown control allows more than one decimal separators?
I don't see a NumericUpDown control there. You cast sender to Textbox, and a textbox will certainly take whatever you type into it. Are you casting a NUD to a Textbox?
The .Text property in a NUD doesn't have to be a number, only the Value property does.
My usual boring signature: Nothing
-
Oct 13th, 2021, 10:34 AM
#4
Re: Why NumericUpDown control allows more than one decimal separators?
The editable part of a NumericUpDown control is a TextBox. As with a TextBox, you can handle KeyDown and restrict input
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 13th, 2021, 01:46 PM
#5
Re: Why NumericUpDown control allows more than one decimal separators?
I think I once knew that, but the end result is WHY? Handling keypresses in textboxes is already problematic (you can get data into the textbox without a keypress), so why do so in a NUD?
My usual boring signature: Nothing
-
Oct 13th, 2021, 04:18 PM
#6
Re: Why NumericUpDown control allows more than one decimal separators?
NumericupDowns will only accept the correctly formatted input, but the value doesn’t change until the input is validated. At least that’s my experience with them.
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 13th, 2021, 04:20 PM
#7
Re: Why NumericUpDown control allows more than one decimal separators?
There are loads of numeric textboxes in the vb.net codebank
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 13th, 2021, 04:32 PM
#8
Re: Why NumericUpDown control allows more than one decimal separators?
Yeah, I know there are at least a few. One thing that I've never gone looking for is how they handle copy/paste.
My usual boring signature: Nothing
-
Oct 13th, 2021, 05:36 PM
#9
Re: Why NumericUpDown control allows more than one decimal separators?
Originally Posted by Shaggy Hiker
Yeah, I know there are at least a few. One thing that I've never gone looking for is how they handle copy/paste.
It’s easy handling paste…
https://www.vbforums.com/showthread....numericTextbox
- Coding Examples:
- Features:
- Online Games:
- Compiled Games:
-
Oct 15th, 2021, 06:09 PM
#10
Thread Starter
Hyperactive Member
Re: Why NumericUpDown control allows more than one decimal separators?
Hopefully that will be fixed in VS 2022.
-
Oct 15th, 2021, 07:12 PM
#11
Re: Why NumericUpDown control allows more than one decimal separators?
I think it is safe to say that it won't be, as it's not seen as a problem. It has almost certainly been this way since 2002.
What does it matter? What scenario are you finding where it makes any difference? You showed a bit of code, but that didn't look quite relevant in light of some of the replies.
My usual boring signature: Nothing
-
Oct 15th, 2021, 07:22 PM
#12
Re: Why NumericUpDown control allows more than one decimal separators?
Originally Posted by Spybot
Hopefully that will be fixed in VS 2022.
There's nothing to fix. It's working as intended. If you're expecting any significant changes to Windows Forms now or in the future, I suggest that you reset your expectations.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|