how to highlight editbox text when press ´return´ using findjobj?
조회 수: 4 (최근 30일)
이전 댓글 표시
Hy, I want to highlight an editbox text when i press 'return' in an other edit box. I managed to find out that this is only possible when using: findjobj.m
I simplyfied a GUIDE GUI in order to test that. I have now two editbox (edit1 and edit2) (See files attached). Unfortunatelly, there is no response to the press key and as well no error.
In addition I would like to have the edit1 text already selected when i start the GUI. How to do that?
댓글 수: 0
채택된 답변
Geoff Hayes
2014년 8월 27일
Sejo - you can try the following by using the uicontrol function to give focus to - and so highlight - a particular widget.
In test_OpeningFcn function, add the following as the last line
uicontrol(handles.edit1);
When you run your GUI, the edit1 should have focus and its text highlighted in blue.
Now create and assign two callbacks to edit1 and edit2 as edit1_Callback and edit2_Callback respectively, with the following code
function edit1_Callback(hObject, eventdata, handles)
uicontrol(handles.edit2);
function edit2_Callback(hObject, eventdata, handles)
uicontrol(handles.edit1);
I commented out the code in your KeyPressFcn and then tried out the following - after typing in some data in edit1, I pressed enter. edit2 immediately had focus and its text was highlighted in blue. I then did the same from edit2 - typed in some data, pressed enter, and now edit1 had focus with its text highlighted in blue.
I guess that the (say) edit1_Callback is called whenever you press enter or the widget loses focus. So the above will work, but may have some undesirable affects especially as you add more widgets to your GUI. You may need to wrap the uicontrol invocation in some sort of if statement to only invoke if the user pressed enter/return while in that widget
function edit2_KeyPressFcn(hObject, eventdata, handles)
switch eventdata.Key
case 'return'
handles.edit2ReturnPressed = true;
guidata(hObject,handles);
end
function edit2_Callback(hObject, eventdata, handles)
if isfield(handles,'edit2ReturnPressed')
if handles.edit2ReturnPressed
handles.edit2ReturnPressed = false;
guidata(hObject,handles);
uicontrol(handles.edit1);
end
end
And do something similar for edit1.
I tested the above with R2014a on a Mac.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Migrate GUIDE Apps에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!