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

3 Ansichten (letzte 30 Tage)
Mehdi
Mehdi am 19 Mai 2014
Bearbeitet: Mehdi am 23 Mai 2014
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.

Antworten (4)

Mehdi
Mehdi am 23 Mai 2014
Bearbeitet: Mehdi am 23 Mai 2014
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 am 19 Mai 2014
Try using the KeyPressFnc and in there compare the length of the string, then change the focus!

Mehdi
Mehdi am 20 Mai 2014
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 am 22 Mai 2014
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

Kategorien

Mehr zu Startup and Shutdown finden Sie in Help Center und File Exchange

Tags

Produkte

Community Treasure Hunt

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

Start Hunting!

Translated by