开发者

Limiting string length in a Edit Box on Matlab user interfaces

I inserted an Edit box in a Matlab user interface and I would like to limit the number of characters that an user can type. There is no obvious property on the Edit box (such as "max characters"). I tried to use the callback function, verifying if the current string size on the edit box was bigger than a limit I set and truncating the first characters, however the callback only acted when I clicked outside the edit box and then inside again.

Do you have any idea of how to do this?

Thanks in advance.

EDITED

As suggested by Amro, I tried placing a verification code inside the KeyPressFcn callback of the edit box. I typed the following code:

function prefix_edit_KeyPressFcn(hObject, eventdata, handles)
    text = get(hObject, 'String');
    if length(text) > 15
        set(hObject, 'String', text(1:15));
    end

The problem is that the edit box string is only changed when I type something, press Enter and then try to type something again.开发者_运维百科 It seems that the KeyPressFcn is only called after pressing Enter (as mentioned in the forum post that Amro suggested).

The solution proposed in the forum seems too complicated, for such a simple task. Surely there has to be a more elegant way...


Instead of using KeyPressFcn, implement the aforementioned callback function on the KeyTypedCallback property of the underlying Java component, which can be found using the findjobj utility.

Note: do NOT use the underlying Java component's document's lineLimit attribute, since this is a blind alley - a remnant of old Java versions that are not used by Matlab.

You could set your own Document object, but the callback way is simpler I think.


Try to put your logic inside the KeyPressFcn callback function. There is an old newsgroup thread discussing a similar solution.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜