TextEdit - How to control character input

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

TextEdit - How to control character input

bachinger software-2
First my problem!
In a dialog the user should input float numbers. How to allow only
digits, $. and $, characters to type into a TextEdit.

This works fine but!!
Using the isNumeric flag works fine but this does not allow the colon
$, in the numeric block of the german keyboard layout.

This works also but!
I subclassed TextEdit and redefined the #onKeyTyped: message.
Now I have control but also I have to handle all the different
key strokes, what I don't like to do and also Subclassing blows up
the system.

Example: "But no Del Backspace Arrow ... and so on"
onKeyTyped: aKeyEvent
(self presenter topShell acceptFloatChar: aKeyEvent)
        ifTrue: [^self defaultWindowProcessing: aKeyEvent]
        ifFalse: [^false].

What I want.
Is there any possibility to use a trigger event instead.

Thanks in forward


Reply | Threaded
Open this post in threaded view
|

Re: TextEdit - How to control character input

Ian Bartholomew-17
> In a dialog the user should input float numbers. How to allow only
> digits, $. and $, characters to type into a TextEdit.
[]
> What I want.
> Is there any possibility to use a trigger event instead.

I don't think so, at least not in the way I think you mean.

One solution might be the MaskedEdit class. This relies on a commercial COM
component being available though and, as my system is not allowed to use it,
I can't tell you a lot more.

Another solution would be to use a new Presenter in place of
NumberPresenter.  For example -

TextPresenter subclass: #MyNumberPresenter

MyNumberPresenter>>onViewOpened
    super onViewOpened.
    self view updatePerChar: true

MyNumberPresenter>>onValueChanged
    | bad caretPos |
    caretPos := self view caretPosition.
    bad := self model value reject: [:each | '0123456789.,' includes: each].
    bad isEmpty ifTrue: [^self].
    Sound bell.
    self model value: (self model value copyWithoutAll: bad).
    self model value size + 2 = caretPos
        ifTrue: [self view caretPosition: caretPos]
        ifFalse: [self view caretPosition: caretPos - 1]

The main difference is that the Model for this Presenter is a String so you
will have to do the conversions yourself.  This might be an advantage as you
can then control whether a , is used. As a demo

s := MyNumberPresenter showOn: '123,456,789.098'

opens a demo Shell with the above number visible.  Edit the text, it now
only allows digits $. and $, and then close the Shell. Evaluating

Number fromString: (s value copyWithout: $,)

should answer the new Number

Regards
    Ian