Page 1 of 3 123 LastLast
Results 1 to 40 of 89

Thread: VB - An ActiveX control which restricts a textbox to numbers

Hybrid View

  1. #1

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    VB - An ActiveX control which restricts a textbox to numbers

    The attached control is a modified TextBox which restricts the user to numbers. The control also has these additional properties:

    CanHaveDecimals - A Boolean property that determines if the number can have decimals

    CanBeNegative - A Boolean property that determines if the number can be negative

    MaxDecimals - Allows the maximum number of decimals to be set

    MinValue - Allows the minimum value to be set

    MaxValue - Allows the maximum value to be set

    DecimalSeparator - Determines which character ("." or ",") is used as the decimal separator

    RequireLeadingDigit (New) - Determines if at least one digit must preceed the decimal separator.

    The code for the control is included in the zip file.

    NOTES:
    • Version 1.1: Updated to include most of the normal properties associated with a textbox. Also includes the bug fix provided by dee-u.
    • Version 1.2: Add Click event
    • Version 1.3: Add SelStart, SelLen and SelText properties
    • Version 1.4: Corrected implementation of SelStart, SelLen and SelText and also added several other properties
    • Version 1.5: Changed the names of the NumberBoxClick and NumberBoxKeyPress events to Click and KeyPress. Added KeyUp, KeyDown and Change events, and corrected DblClick event
    • Version 1.6: 12/02/2006 Corrected KeyUp, KeyDown, and KeyPress properties by adding standard parameters
    • Version 1.7: 02/07/2007 Added Enums for Alignment and BorderStyle properties. Corrected problem where numberbox behaved as if it was locked when data with decimal points was added, and added protection against pasting in invalid characters (which I thought it already had)
    • Version 1.8: 02/07/2007 Added support for paste via ctrl-V and allow for an almost unlimited number of decimals
    • Version 1.9: 06/11/2007 Added MinValue and MaxValue properties
    • Version 1.10: 02/27/2008 Added DecimalSeparator property. Also fixed a bug which occurred when more than the allowed number of decimals was entered
    • Version 1.11: 03/14/2008 Changed code so that an attempt to add a second decimal separator is ignored rather than causing the first one to be removed. Corrected bug where adding a decimal separator to a negative number would cause one too few decimal places to be shown.
    • Version 1.12: 04/25/2008 Corrected MaxValue and MinValue processing when comma is the decimal separator. Changed the Appearance property values so that they display Flat and 3D like the normal textbox rather than 0 and 1
    • Version 1.13: 05/02/2008
      • Corrected MinValue and MaxValue processing so that the entry of a minus sign doesn't result in a type mismatch error.
      • Changed MinValue and MaxValue processing so that if a change would violate either limit, the old value is restored rather than changing it to the MinValue or MaxValue.
      • Corrected spelling of DecimalSeparator property.
      • Added a Test project for those who might want to debug my code.
    • Version 1.14: 10/20/2008
      • Made .text the default property.
      • Added categories to properties
    • Version 1.15: 03/29/2009 Corrected problem where previously if you had a number that contained a decimal separator, and you selected the number with the intention of entering a new number that started with a decimal separator, the entry of the decimal separator was not allowed because it was assumed you were trying to add a second decimal separator.
    • Version 1.16: 04/03/2009
      • Added RequireLeadingDigit property. For example .15 would be changed to 0.15 if set to True.
      • Removed MaxLength and PasswordChar properties
      • Changed behavior where if number was greater than the MaxValue or less than the MinValue it dropped the last digit. New behavior changes the value to MaxValue or MinValue as appropriate.


    KNOWN PROBLEMS: None
    Attached Files Attached Files

  2. #2
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: VB - An ActiveX control which restricts a textbox to numbers

    I fixed a small flaw in the above project, here is the updated version...

    [Edited by MartinLiss]dee-u's code is now out of date.
    Attached Files Attached Files
    Last edited by MartinLiss; Mar 9th, 2006 at 09:47 PM.
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  3. #3
    Frenzied Member litlewiki's Avatar
    Join Date
    Dec 2005
    Location
    Zeta Reticuli Distro:Ubuntu Fiesty
    Posts
    1,162

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Heyy i found something interesting .Maybe this can reduce the size of the code in some way
    VB Code:
    1. //module
    2. Public Const GWL_STYLE = (-16)
    3.  
    4. ' Restricts input to the edit control to digits only
    5. Public Const ES_NUMBER = &H2000
    6.  
    7. Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" _
    8.                               (ByVal hwnd As Long, ByVal nIndex As Long, _
    9.                               ByVal dwNewLong As Long) As Long
    10.  
    11.  
    12. Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" _
    13.                             (ByVal hwnd As Long, ByVal nIndex As Long) As Long
    14. //end module
    form code
    VB Code:
    1. Private Sub Form_Load()
    2.   SetWindowLong Text1.hwnd, GWL_STYLE, _
    3.                           GetWindowLong(Text1.hwnd, GWL_STYLE) Or _
    4.                           ES_NUMBER
    5.  
    6. End Sub
    __________________
    ________________0îîî___
    ___îîî0________(___)____
    __(___)_________) _/_____
    ___\_ (_________(_/______
    ____\_)_________________

  4. #4
    New Member
    Join Date
    Jan 2007
    Posts
    6

    Re: VB - An ActiveX control which restricts a textbox to numbers

    i need the last version of the numberbox for excel 2003, and the instructions how to install it.

    thanks

  5. #5

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Quote Originally Posted by mkkmkk
    i need the last version of the numberbox for excel 2003, and the instructions how to install it.

    thanks
    Sorry but my ocx has nothing to do with excel.

  6. #6
    New Member
    Join Date
    Jan 2007
    Posts
    6

    Re: VB - An ActiveX control which restricts a textbox to numbers

    themks

  7. #7
    New Member
    Join Date
    Jan 2007
    Posts
    6

    Re: VB - An ActiveX control which restricts a textbox to numbers

    thanks, why doesn´t work in a form of excel?

  8. #8

  9. #9
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    I tried out V 1.7 in my 'Numbers To English' program (in [RESOLVED] VB6/ Number to Words / 66 digits) . It works very well (good job!) except the limit on digits to the right of the decimal point. In theory, the number of digits (to the right) could be the maximum the textbox will take (in theory!). I'm sure it won't often happen.

    But sometimes, in 'normal' apps, the number of decimal digits will not be know at design time.

    Is there a reason you are limiting decimals to a defined design-time value? If so, would I need to add one more allowable decimal digit each time the user adds one to the control? Is there a point at which it'll blow up?

    Either way, it seems like a lot of overhead.

    Also, the paste-into fuctionality isn't working
    Last edited by rjbudz; Feb 7th, 2007 at 02:20 PM.

  10. #10

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: VB - An ActiveX control which restricts a textbox to numbers

    I'm not limiting the decimals. I believe you over looked the MaxDecimals property. I'll look into the paste problem, thanks.

  11. #11
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Setting the MaxDecimals property is, well, setting the maximum number of decimals. The object is to NOT set a maximum number of decimals and allowing the user to enter them to his/her heart's content!

    Did I miss something?

  12. #12

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Quote Originally Posted by rjbudz
    Setting the MaxDecimals property is, well, setting the maximum number of decimals. The object is to NOT set a maximum number of decimals and allowing the user to enter them to his/her heart's content!

    Did I miss something?
    Okay I think I misunderstood you. I felt I was not limiting it because you can use the MaxDecimals in the IDE or in code to increase the maximum, but now I see that you don't want even that "restriction". I will try to change it so that it can be unlimited by default.

  13. #13
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    The tricky part will be how to set MaxDecimals to infinity. Setting to zero means no decimals.

  14. #14
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: VB - An ActiveX control which restricts a textbox to numbers

    I like the decimal restriction. My nbrboxes are too small to have the user going overboard on decimal places. I've also an whole upper number limit so that the number is never greater than the nbrbox size.
    It is a user input box and I doubt that anyone will spend time adding or pasting in more than 9,999 decimal places. A numberbox that is 10,000 characters would also need to be pretty big.
    If you are writing to the nbrbox round the number to 9,999th decimal place or there abouts.
    Last edited by sgrya1; Feb 7th, 2007 at 03:17 PM.

  15. #15
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    You're probably right. I doubt it will ever be a problem

    The textbox maxes out at 65,535 (me thinks, though some sources say 32,000) characters. 10,000 isn't bad. I suppose, all things considered, that's a fair limit.

    I'm really amazed this hadn't been done defore. Is seems so obvious in hind-sight. I'm certain it'll be used far & wide.

    Nicely done.


    Just-a-waiting on that paste thing ( ) ...
    Last edited by rjbudz; Feb 7th, 2007 at 03:23 PM.

  16. #16
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    No, don't worry. I was just suggesting how you might implement the infinite decimal allowance part of your control.

    Just trying to be of assistance. There's nothing wrong.

  17. #17

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Right now I have it working in my copy this way. If you blank out the MaxDecimals it changes it to 9999. Is that acceptable?

  18. #18
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Sure, but I could set MaxDecimal to 9999 anyway, hence the added code to automatically set it to 9999 isn't really neccessary.

    So, the short answer is: If infinity is out of the question, just leave is the way it is in the last release. As you've written it, any value entered in excess of 9999 automatically sets the vale to it (9999), as would be expected.

    My intent isn't to get you to re-write it for my benefit. I like it just fine (except for the paste-thing ) I just suggested ASSUMED infinity for decimals.
    Last edited by rjbudz; Feb 7th, 2007 at 07:51 PM.

  19. #19

  20. #20
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Nice. Does it fix the paste problem?

    And, oh BTW, I couldn't let the textbox limit ambiguity rest ('tis my nature), so I tested it with a single line and multi-line textbox. It took overnight to run, the multi-line textbox seemed to take longer and longer to calculate textbox2 = textbox2 & "X". Textbox1 (single line) didn't seem to take longer. (No I'm not going to run timers!)

    Anyway, both textboxes max out at 65,535. This is in direct contrast to what Microsoft stated. Also, there is no error message when the max is reached. Instead it just doesn't add anymore chars (as opposed to dropping off the first ones to accommodate the last).

    I know everyone is going to sleep better knowing this!

  21. #21

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Quote Originally Posted by rjbudz
    Nice. Does it fix the paste problem? ....
    If you mean can you now paste via ctrl-v then yes. If you mean something else then please let me know.

  22. #22
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Nope. That is what I meant. I rate Excellent! Thank you!

  23. #23
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Hi again.

    I noticed that if a user enters a second decimal point, the first decimal point disappears.

    The user will probably not notice this event (I didn't) and will continue to happily enter numbers, and then click the ‘Go’ button. The result: Instead of entering 3.1415926535, he/she will have entered 314159265.35, and obtain somewhat different results than expected.

    You may want to notify the user that a second decimal point has been entered. Ignoring the second one (the same way the control ignores non-numeric chars) won't work as the user may have made the mistake when entering the first one.

  24. #24

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Quote Originally Posted by rjbudz
    Hi again.

    I noticed that if a user enters a second decimal point, the first decimal point disappears.

    The user will probably not notice this event (I didn't) and will continue to happily enter numbers, and then click the ‘Go’ button. The result: Instead of entering 3.1415926535, he/she will have entered 314159265.35, and obtain somewhat different results than expected.

    You may want to notify the user that a second decimal point has been entered. Ignoring the second one (the same way the control ignores non-numeric chars) won't work as the user may have made the mistake when entering the first one.
    I see what you are saying but there are a lot of possible mistakes and I think the user would find it annoying to be notified of each one.

  25. #25
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    I suppose it's a matter of opinion.

    I think entering a value of 3.1415926535 and having 314159265.35 entered instead would be far worse than asking if this is what the user intended. Entering two decimal points IS a mistake. To have the control decide the correct course of action (eliminate the first one) is presumptuous.

    Users don't always recheck values they've entered, more so in this case because he/she knows non-numeric values will be ignored. Not advising of a gross error defeats the purpose (MHO).

    The vast majority of mistakes a user can make (I can’t think of any others you haven’t anticipated) using this control have been eliminated. That's what makes it so useful in the first place!

  26. #26

  27. #27
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    sgrya1 makes a valid point regarding the second decimal point. I think that is probably the way to go with it (again, MHO).

  28. #28

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Quote Originally Posted by rjbudz
    sgrya1 makes a valid point regarding the second decimal point. I think that is probably the way to go with it (again, MHO).
    I agree and I'll make the change in the next release.

  29. #29
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Here's the code that does it from Calculator.zip:

    VB Code:
    1. Private Sub ButtonDecPoint_Click()
    2.     If bError = False Then
    3.         If iCurrentRadix = DECNUM Then
    4.             If bEntered = False Then    '   Start of New Number
    5.                 bEntered = True
    6.                 OutputWindow.Text = "."
    7.             Else                        '   Append
    8.                 If InStr(OutputWindow.Text, ".") = 0 Then   '   Make Sure Only 1 Decimal Point
    9.                     OutputWindow.Text = OutputWindow.Text + "."
    10.                 End If
    11.             End If
    12.         End If
    13.     End If
    14.     OutputWindow.SetFocus
    15. End Sub

  30. #30
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: VB - An ActiveX control which restricts a textbox to numbers

    In particular:

    VB Code:
    1. If InStr(OutputWindow.Text, ".") = 0 Then   '   Make Sure Only 1 Decimal Point
    2.                     OutputWindow.Text = OutputWindow.Text + "."
    3.                 End If

  31. #31
    Hyperactive Member rjbudz's Avatar
    Join Date
    Jul 2005
    Location
    San Diego
    Posts
    262

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Of course. I included the entire method to make it easier to find and understand its use!

  32. #32

  33. #33
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: VB - An ActiveX control which restricts a textbox to numbers

    I've just moved to Europe where they use a comma "," as a decimal point rather than a "."

    If I change the language to English it's fine but there's a problem with Polish for example.

    You probably don't want to have to think about this but how do most windows apps understand that a comma acts as a decimal point.

    The numberbox obviously doesn't allow it's use.

  34. #34

  35. #35
    Fanatic Member
    Join Date
    Sep 2006
    Location
    London, UK
    Posts
    816

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Martin,

    Can you ignore my last request. When you press . on the numeric keypad it comes up as a comma. I think they are well aware that other countries use a . and that occasionally they need to use the keypad . rather than the numeric pad "."

    I do have one more question though. How do I handle an empty numberbox.

    if nbrbox. is empty then
    if val(nbrbox) is nothing

    if nbrbox.text = "" doesn't work as the contents aren't a string value.

  36. #36

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Please, try to digit the following number

    -123.4567

    in the NumberBox at run-time in a form with MaxDecimals=2
    and look what happens...

    -7123.45
    Last edited by aleall; Feb 27th, 2008 at 08:01 AM.

  37. #37

    Thread Starter
    Former Admin/Moderator MartinLiss's Avatar
    Join Date
    Sep 1999
    Location
    San Jose, CA
    Posts
    33,427

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Updated to Version 1.10 which corrected the bug reported by aleall and added a DecimalSeperator property so that the user can decide to show numbers in the American (1234.56) or European (1234,56) style.

  38. #38
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Hey martin, the ff. will break the decimal value, sorry I haven't had time to tinker on the solution...

    Code:
    Private Sub Command1_Click()
        'will display: -.1
        With NbrTextBox1
            .Text = "123456789"
            .SelStart = 0
            .SelText = "-"
            .SelStart = 0
            .SelText = "."
        End With
    End Sub
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

  39. #39

  40. #40
    Software Carpenter dee-u's Avatar
    Join Date
    Feb 2005
    Location
    Pinas
    Posts
    11,123

    Re: VB - An ActiveX control which restricts a textbox to numbers

    Quote Originally Posted by MartinLiss
    I'm not sure what you are saying the problem is. What is "ff"?
    The code I posted will only show 1 numeric value after the decimal, I just coded the way I did it manually...
    Regards,


    As a gesture of gratitude please consider rating helpful posts. c",)

    Some stuffs: Mouse Hotkey | Compress file using SQL Server! | WPF - Rounded Combobox | WPF - Notify Icon and Balloon | NetVerser - a WPF chatting system

Page 1 of 3 123 LastLast

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