NumberField Control

NumberField is a subclass of EditField that allows the user to only type numbers. To use it, import the NumberField.rbo control into your project and then drag it to a window (you’ll find it in the Project Controls section of the control list). There is only one property, which you can modify in the IDE Property Editor or in code:

AllowDecimals As Boolean: Set to True (or check in the IDE) to allow decimal numbers to be entered.  Otherwise only integers can be entered.

This is a simple control, but I’ve found it useful from time-to-time.

Download NumberField Control



6 Responses to “NumberField Control”

  1. Use this improved code in the keydown event – it prevents leading zeros being entered and correctly uses the up and down arrow keys:

    Dim skipKey As Boolean = True

    If Key >= “0″ And Key <= “9″ Then skipKey = False

    If AllowDecimals Then
    If Key = “.” And Not IsDecimalSet Then skipKey = False
    End If

    If Me.Text = “” And Key = “-” Then skipKey = False

    If Key = Chr(8) Then skipKey = False ‘ BackSpace
    If Key = Chr(4) Then skipKey = False ‘ End
    If Key = Chr(1) Then skipKey = False ‘ Home
    If Key = Chr(127) Then skipKey = False ‘ Delete
    If Key = Chr(28) Then skipKey = False ‘ Left arrow
    If Key = Chr(29) Then skipKey = False ‘ Right arrow
    If Key = Chr(30) Then skipKey = False ‘ Up arrow
    If Key = Chr(31) Then skipKey = False ‘ Down arrow
    If Key = Chr(9) Then skipKey = False ‘ Tab

    Dim userKeyDown As Boolean
    userKeyDown = KeyDown(Key)

    If userKeyDown Then
    skipKey = userKeyDown
    End If

    // it’s a number but it could be a leading zero like 05
    if me.SelStart = 0 and Key = “0″ then skipKey = True

    Return skipKey

  2. joe says:

    Just a quick question…I’m new to REALbasic and really appreciate this NumberField code but I cannot figure out where and how to set AllowDecimals=TRUE.

    I tried using it in a button but all I get is “This method or property does not exit” so I guess I don’t understand how/where to set it TRUE.

    Any help would be appreciated!

    Joe

  3. Paul Lefebvre says:

    There are two ways to set this property after adding the control to a window:

    1. In the property list to the right you will see a value called “Allow Decimals” in the Behavior section. Simply check it.

    2. In the Open event of the control, add the line Me.AllowDecimals = True

  4. joe says:

    Thank you very much, Paul! Odd…when you put the stuff in the right place it works better.

    I am trying to understand this OOP stuff: reading all the books I can find about REALbasic, and searching for all the online tutorials that I can find but it is so helpful when others, like yourself, take the time to answer what must seem so obvious.

    Thanks.

  5. Mark Hartman says:

    The added line:

    // it’s a number but it could be a leading zero like 05
    if me.SelStart = 0 and Key = “0″ then skipKey = True

    will not allow any leading zeros. The user should be allow to enter at least one zero but not more than one after that.

  6. Roger Meier says:

    I have a slight improvement for the KeyDown event code that may be useful.

    The modified version allows the “-” to be typed at any time, not only when the editfield is empty, which is more intuitive. Also, it prevents numbers from being typed in front of the “-” sign, if there is one.

    // Allow all numbers, but not in front of a “-”
    If Key >= “0″ And Key 0) Then skipKey = False

    // Only allow a “-” to be typed in at position 0
    if Key = “-” and me.SelStart = 0 and instr(me.Text, “-”) <= 0 then skipKey = False

Leave a Reply