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?

采纳的回答

Geoff Hayes
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
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.
Kah Joon Yong
Kah Joon Yong 2015-10-14
It works. But the charPressCount field can't really keep track of the character counting. My biggest problem was if I highlight the whole text and 'backspaced' it, or just highlight part of it, I can't really keep track of how many characters left in the box. The other problem occurs when I want to allow the cursor to move back to editbox1 if I pressed backspace in an empty editbox2. It has a whole bunch of problems and finally I just gave up and use a static text box instead. The user type the whole sentence in the edit box and the program broadcast it to a textbox after sorting out the first 20 characters in the first textbox and the rest in the second textbox.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 App Building 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by