Results 1 to 12 of 12

Thread: Why NumericUpDown control allows more than one decimal separators?

  1. #1

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    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:
    1. Dim DecimalSeparator As String = Application.CurrentCulture.NumberFormat.NumberDecimalSeparator
    2.  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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    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.

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  4. #4
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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

  5. #5
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  6. #6
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    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.

  7. #7
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Why NumericUpDown control allows more than one decimal separators?

    There are loads of numeric textboxes in the vb.net codebank

  8. #8
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,464

    Re: Why NumericUpDown control allows more than one decimal separators?

    Quote Originally Posted by Shaggy Hiker View Post
    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

  10. #10

    Thread Starter
    Hyperactive Member Spybot's Avatar
    Join Date
    Jan 2019
    Posts
    329

    Re: Why NumericUpDown control allows more than one decimal separators?

    Hopefully that will be fixed in VS 2022.

  11. #11
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,988

    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

  12. #12
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,297

    Re: Why NumericUpDown control allows more than one decimal separators?

    Quote Originally Posted by Spybot View Post
    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
  •  



Click Here to Expand Forum to Full Width