Please help - How can i limit the number of inputs in the editbox (GUI)

1 回表示 (過去 30 日間)
Mehdi
Mehdi 2014 年 5 月 19 日
編集済み: Mehdi 2014 年 5 月 23 日
Hi all, Thank you for reading my question. I have a GUI with a couple of editboxes. I want to limit the number of input characters for each box so that when 5 digits are entered it stops accepting further inputs and automatically moves the cursor to the next editbox. I'd be very grateful if you could help. Mehdi
P.S. I have no knowledge of java.

回答 (4 件)

Mehdi
Mehdi 2014 年 5 月 23 日
編集済み: Mehdi 2014 年 5 月 23 日
Your code gives the error: Too many input arguments
I actually came up with the following code which does move the cursor automatically to the next editbox after entering 5 characters:
function edit1_KeyPressFcn(hObject, eventdata, handles)
if strcmp(eventdata.Key, 'backspace');
handles.edit1 = handles.edit1(1:end-1)
elseif isempty(eventdata.Character)
return
else
handles.edit1 = [handles.edit1 eventdata.Character]
end
stred1 = char(handles.edit1)
pure_stred1 = stred1(isstrprop(stred1,'alphanum'))
leng_str1 = length(pure_stred1)
if leng_str1>=5
uicontrol(handles.edit2) % move the cursor to the next editbox
end
guidata(gcbf, handles)
but it has three problems:
  1. If I copy and past (Ctrl+v) input of multiple characters, it understands it as one character.
  2. If I select the whole previously entered input and click 'backspace' or 'space' it also understands it as one-character input.
  3. If I delete only one character by using the button 'delete', it understands it as deletion of whole inputs so it sets the length to zero.
Any idea on how to make the code understand the actions as they are meant to?

Roberto
Roberto 2014 年 5 月 19 日
Try using the KeyPressFnc and in there compare the length of the string, then change the focus!

Mehdi
Mehdi 2014 年 5 月 20 日
Thank you Roberto. I put
text = get(hObject, 'value') length(text)
in the KeyPressFnc but all I get is 0 and 1 for each character that I enter in the editbox. What am I missing here?

Roberto
Roberto 2014 年 5 月 22 日
Try using the 'String' property instead of the 'Value' one:
function edit1_KeyPressFnc(hObject,~)
strEdit = get(hObject,'String');
if lenght(strEdit) >= 6
%whatever code you like
end

カテゴリ

Help Center および File ExchangeStartup and Shutdown についてさらに検索

タグ

製品

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by