On Mon, Jun 13, 2011 at 8:48 AM,  <[email protected]> wrote:

> PS: The code is attached - a test dialog for sorting out how to check for
> valid numeric entry. It's pretty messy I'm afraid 'cos I'm doing a couple of
> other things as well.

Oliver,

I reworked you program to restrict the user to entering a decimal
number with no more than 2 decimal places.  It is a pretty good
implementation.

The code is formatted so it shouldn't wrap.  For your reference:

/**
 * Shows how prevent the user from entering invalid
 * characters in an edit control.  In this case the
 * user is resctricted to entering a decimal number
 * with no more than 2 decimal places.
 */

.EditDecs~newInstance

::requires ooDialog.cls


::class EditDecs subclass ResDialog

  ::method newInstance class
    dlg = self~new("EditDecs.dll", IDD_EDITDECS,           -
                   dlgData., "EditDecs.h")
    dlg~activate


  ::method activate unguarded
    r = self~execute("SHOWTOP")
    say "EditDecs-activate-02: r =" r

  ::method initDialog
    expose ecDecNum1 ecDecNum2 decNum2Prev inSetText

    ecDecNum1 = self~newEdit("IDC_DECNUM1")
    ecDecNum2 = self~newEdit("IDC_DECNUM2")

    self~connectEditEvent("IDC_DECNUM1", "GOTFOCUS",       -
                          onGotFocus)

    self~connectEditEvent("IDC_DECNUM1", "LOSTFOCUS",      -
                          onLostFocus)

    self~connectEditEvent("IDC_DECNUM2","UPDATE")

    decNum2Prev = ''
    inSetText = .false

  ::method onUpdate unguarded
    expose ecDecNum2 decNum2Prev inSetText

    text = ecDecNum2~getText()

    if inSetText then do
      inSetText = .false
      return
    end

    if \ self~isValidNumber(text, ecDecNum2) then do
      pos = self~getCaretPos(ecDecNum2) - 1
      inSetText = .true
      ecDecNum2~setText(decNum2Prev)
      ecDecNum2~select(pos, pos)
      return self~badChar(ecDecNum2)
    end
    else do
      decNum2Prev = text
    end

  ::method isValidNumber private
    use strict arg number, edit

    haveDecimal = .false
    places = 0

    do i = 1 to number~length
      n = number~substr(i, 1)

      if haveDecimal then do
        if n == '.' then
          return self~setBalloonMsg(places)

        places += 1
        if places > 2 then
          return self~setBalloonMsg(places)
      end

      if \ self~isDecimalDigit(n) then
        return self~setBalloonMsg(places)

      if n == '.' then
        haveDecimal = .true
    end

    return .true

  ::method setBalloonMsg private
    expose balloonMsg
    use strict arg places

    if places >= 2 then
      balloonMsg = "Only two decimal places are allowed."
    else
      balloonMsg = "You can only type a number here."

    return .false

  ::method badChar private
      expose balloonMsg
      use strict arg edit

      edit~showBalloon("Unacceptable Character",           -
                       balloonMsg, "ERROR")
      return 0

  ::method isDecimalDigit private
    use strict arg n

    if n >= 0, n <= 9 then return .true
    if n == '.' then return .true
    return .false

  ::method getCaretPos private
    use strict arg edit

    d = edit~selection

    if d~startChar == d~endChar then
      return d~startChar
    else
      return d~endChar

  ::method onLostFocus unguarded
    say "Lost Focus IDC_DECNUM1."

  ::method onGotFocus unguarded
    say "Got Focus IDC_DECNUM1."

--
Mark Miesfeld

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
Oorexx-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/oorexx-users

Reply via email to