MaskedEdit & Float

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

MaskedEdit & Float

Osvaldo Aufiero-2
Hi people,
I am having some trouble with the MaskedEdit control...
In fact what I need is to control the user to enter a float or an integer,
and a valid one... what do you recommend? is Maskedit control the way?
   For example: 4.5 and not: 4.5. or 45...5.,.,,.a

TIA

Osvaldo


Reply | Threaded
Open this post in threaded view
|

Re: MaskedEdit & Float

Ian Bartholomew-18
Osvaldo,

There are a number of ways of doing this, a new TypeConverter for
example, but the easiest is probably to just create a new ValuePresenter
class.  For example

- Add a new ValuePresenter class called ValidatedNumber (or whatever)

- Create a new view for the class.  This will default to a ContainerView
so, in the VC, delete that and drop a TextEdit.Default view from the
toolbox. The onlu change is to set the #updatePerCharacter aspect to
true.  Save the result as the defaultView for ValidatedNumber

- Add two instVars to the ValidatedNumber class named caretPosition and
textBuffer

- Add 3 methods

onValueChanged
    | string |
    string := self value.
    (string allSatisfy: [:each | '0123456789.-' includes: each])
            ifFalse: [^self revert].
    string isEmpty
        ifFalse:[
            ((string includes: $-) and: [string first ~= $-])
                ifTrue: [^self revert].
            (string occurrencesOf: $-) < 2
                ifFalse: [^self revert].
            (string occurrencesOf: $.) < 2
                ifFalse: [^self revert]].
    textBuffer := string.
    caretPosition := self view caretPosition.
    ^super onValueChanged

onViewOpened
    super onViewOpened.
    textBuffer := self value.
    caretPosition := self view caretPosition

revert
    Sound bell.
    self value: textBuffer.
    self view caretPosition: caretPosition - 1

You can try it out by evaluating ValidatedPresenter show (you can
resize it before editing).  You should only, unless I've missed out some
test, be able to enter valid Integer and Float values (only using a
normal x.y format).

You can use the resulting class anywhere you would normally use a
NumberPresenter but when you want to populate/read the control you will
have to do the Text<>Number conversion yourself.

--
Ian

Use the Reply-To address to contact me.
Mail sent to the From address is ignored.