how to highlight editbox text when press ´return´ using findjobj?

2 views (last 30 days)
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?

Accepted Answer

Geoff Hayes
Geoff Hayes on 27 Aug 2014
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.
  1 Comment
sejo
sejo on 27 Aug 2014
Thx. It worked well with both recommended code. I will test it now with my real GUI...
That with findjobj didnt work with R2014a on Win 7

Sign in to comment.

More Answers (0)

Categories

Find more on Migrate GUIDE Apps in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!