|
I'm building a GUI programmatically and I'm noticing some unexpected behavior that I can't figure out how to fix. I have an edit text box and a button next to it such that when the button is pressed, the string from the edit text box gets added to a list box. The button should also clear the edit text box (set the string to ''), and return control to it. Since I didn't want to have to go to the mouse so often, I thought I'd add a KeyPressFcn to catch 'return' and call the push button callback.
The problem I've been having is that when I use 'return' to call the push button callback, empty strings are added to the list box even though they shouldn't be. (The push button, however, works correctly.) This doesn't make any sense to me, since all of the list box updates happen in the push button callback, and the key press function just calls the push button callback.
Can anybody help me figure out this strange behavior? The relevant callbacks are below.
function yaddtolistPush_Callback(src,eventdata)
% Adds a formula typed into yaxisEdit to yaxisList
% Get the entered string and current list
formula=get(yaxisEdit,'String');
currlist=get(yaxisList,'String');
% Check that formula is not empty
if ~strcmp(formula,'')
% Add formula to list
len=length(currlist);
newlist=currlist;
newlist{len+1,1}=formula;
set(yaxisList,'String',newlist);
% Clear from edit box and return control to yaxisEdit
set(yaxisEdit,'String','');
uicontrol(yaxisEdit);
else
uicontrol(yaxisEdit);
end
end
function yaxisEdit_KeyPressFcn(src,eventdata)
% The KeyPressFcn catches 'Enter' so that the user can add formulas
% to the list without using the button
% Get the pressed button
pressed=eventdata.Key;
if strcmp(pressed,'return')
drawnow;
yaddtolistPush_Callback(yaddtolistPush,[]);
end
end
|