Controlling number of Characters typed into Edit Box using KeyPressFcn
2 次查看(过去 30 天)
显示 更早的评论
I want to create an Edit box that only allow let say 5 characters and any extra characters should then switch to the next edit box.
function Name_Line_1_KeyReleaseFcn(hObject, eventdata, handles)
a=get(hObject,'String')
if length(a)> 5
uicontrol(handles.Name_Line_2)
end
However there was a big problem. When I run this without breakpoints, when I type 'Hello, World!' into the edit box, the curso won't jump to the next edit box. Only if I clink on the other uicontrols then back to the edit box, only the cursor jump to the next edit box.
I have placed a breakpoint at the line 'if length(a)>5' and ran it. If let say the Box already had 'Hell' in it, and now I type 'o', debug mode is activated but 'a' only contains 'Hell' not 'Hello'. If I tape the next letter 'w', 'a' will contain 'Hello' instead of 'Hellow'. However if now I give the command
a=get(hObject,'String')
in the command window 'a' contains 'Hellow' correctly. Why didn't it executes the command consistantly in the function as well as in the command window? What should I do?
0 个评论
采纳的回答
Geoff Hayes
2015-10-12
Kah - I think that part of the problem is that the String property of the edit text control is not updated until focus is set to some other control. So no matter how many characters you enter into the text box, your call to
get(handles.edit1,'String')
will always return an empty matrix until focus has been released at this control and focus set to another control.
You can try something like the following which "registers" character key presses in your edit1 control and shifts focus to edit2 after the fifth character key press.
% --- Executes on key press with focus on edit1 and none of its controls.
function edit1_KeyPressFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata structure with the following fields (see UICONTROL)
% Key: name of the key that was pressed, in lower case
% Character: character interpretation of the key(s) that was pressed
% Modifier: name(s) of the modifier key(s) (i.e., control, shift) pressed
% handles structure with handles and user data (see GUIDATA)
if ~isempty(eventdata.Character)
% only register a key press for characters
if ~isfield(handles,'charPressCount')
handles.charPressCount = 0;
end
handles.charPressCount = handles.charPressCount + 1;
if handles.charPressCount == 5
% five character presses have been regisered so move to next
% control
uicontrol(handles.edit2);
handles.charPressCount = 0;
end
guidata(hObject,handles);
end
Note how we check to see if eventdata.Character is empty or not. If the former, then we ignore the key press. Else if the latter, then we increment the counter in handles to keep track of how many character key presses have been made. Once we get to the fifth one, we shift focus to the edit2 control.
Try the above and see what happens!
3 个评论
Geoff Hayes
2015-10-13
Glad it worked! The above line of code just saves the updated handles structure (since we have been incrementing the charPressCount field) so that all other callbacks that receive this structure as an input (or all requests to get this structure) have the most recent version of it. See guidata for more details.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!