TOPIC: limited size of strings
#1157
limited size of strings 2 Years, 4 Months ago
Hello,
an first of all: thank you for this wonderful program!

I want to limit a string to a maximum amount of char.
With "text width" I can specify the size of the box. Sure, I have the possibility to cut the string after 20 chars before I store the string.
But: if I want to store only 20 chars, I want the user only to type in 20 chars, so if he types more, perhaps theres is a beep-beep, so he doesn't type in a short story.

I hope you can help me (sorry for my bad english)

Thomas
 
 
#1158
Re:limited size of strings 2 Years, 4 Months ago
Hi Thomas,

I do not think there is anything to do that - we would normally just check whether a field is valid when it loses focus.

Here is one way you could do it:

Code:


LimitedFieldControl

FieldControl
    {
    New(@args)
        {
        super(@args)
        .width = args.width
        }
    EN_CHANGE()
        {
        s = GetWindowText(.Hwnd)
        if s.Size() > .width
            {
            Beep()
            SetWindowText(.Hwnd, s.Substr(0, .width))
            }
        return super.EN_CHANGE()
        }
    }



This is not perfect because the SetWindowText moves the cursor to the beginning so it behaves strangely if you keep typing.

Another approach would be to intercept the WM_CHAR messages, but then you have to worry about things like paste.

PS. Don't worry about bad english.
 
 
andrew
 
#1159
Re:limited size of strings 2 Years, 4 Months ago
So my problem can't be solved... Anyway, thank you for the answer!
Thomas
 
 
#1160
Re:limited size of strings 2 Years, 4 Months ago
Did you try the code I supplied? It works fine - the problems with it are not so bad. Just put the code in your library and use LimitedField instead of Field.

One of the advantages of Suneido (and other similar languages like Smalltalk) is that it is quite easy to extend.
 
 
andrew
 
#1165
Re:limited size of strings 2 Years, 4 Months ago
Thank you!
I 've just tested the code now - Monday night I was too tired for testing - and it works fine.
I 've just added
Code:

Alert('Stop', 'Beep', flags: MB.ICONSTOP)

after the "Beep" and it is just, what I need.
B)
Thomas
 
 
Last Edit: 2008/04/16 16:14 By Bieser.