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

1 visualización (últimos 30 días)
Mehdi
Mehdi el 19 de Mayo de 2014
Editada: Mehdi el 23 de Mayo de 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.

Respuestas (4)

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

Mehdi
Mehdi el 20 de Mayo de 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 el 22 de Mayo de 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

Categorías

Más información sobre Startup and Shutdown en Help Center y File Exchange.

Etiquetas

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by