GUI - change 'edit' or 'popup' style by number of mouse clicks

3 views (last 30 days)
James Ang
James Ang on 7 Apr 2016
Commented: James Ang on 11 Apr 2016
Hi,
I've a list of historical information: e.g
Amanda
Bob
Carrie
Daniel
that I can populate into the popupmenu where users can select from this list.
However, there are times that the user would like to insert their input, e.g
Emma
by editing in an edit box.
Is it possible select between a popup menu, or an edit box, by the number of mouse clicks?
e.g,
click once --> pop up menu list appears and user selects from them
double click --> pop up menu becomes an edit box and users are then able to insert their data.
Thanks in advance.

Accepted Answer

Geoff Hayes
Geoff Hayes on 10 Apr 2016
James - I couldn't get the popup menu mouse down callback to respond to single or double clicks (this may be intentional with there being no way to intercept these events for this control) but I was able to interact with it if I listen for a particular key. For example, suppose the user has selected the popup menu list and is navigating through it and doesn't find the name that he or she is looking for. If the user then presses a certain key combination, we can then hide the popup menu and display an edit text control that had been previously hidden behind the menu.
This has been tested with R2014a on a Mac, and so I listen for the combination of command and i to insert a new name into the list. In the KeyPressFcn for the popup menu, we have
function popupmenu1_KeyPressFcn(hObject, eventdata, handles)
if ~isempty(eventdata.Modifier) && strcmpi(eventdata.Modifier{1},'command') && ...
strcmpi(eventdata.Key,'i')
set(hObject,'Visible','off');
set(handles.popupedittext,'Visible','on','String','<enter new name>');
uicontrol(handles.popupedittext);
end
So if the user presses command and i then we hide the popup menu (by setting its Visible property to off), show the edit text (by doing the opposite we did for the other control) with some default text, and then we set focus to that control using uicontrol. The user can then type the new name and press a key to indicate when complete. If we use the return key to indicate that the name is ready to be added to the list, then the KeyPressFcn for the edit text box would be
function popupedittext_KeyPressFcn(hObject, eventdata, handles)
if strcmpi(eventdata.Key,'return')
uicontrol(handles.popupmenu1);
set(hObject,'Visible','off');
if ~isempty(get(hObject,'String'))
handles.listOfNames = [handles.listOfNames ; get(hObject,'String')];
set(handles.popupmenu1,'String',handles.listOfNames);
guidata(hObject,handles);
end
set(handles.popupmenu1,'Visible','on','Value',length(handles.listOfNames));
uicontrol(handles.popupmenu1);
end
In the above, we wait for the return key to be pressed and then set focus to the popup menu (this it to ensure that focus leaves the edit text control and the complete string that the user typed is available to us). We then do something very similar to before, hiding one control and showing the other. Only if there is a non-empty string in the edit text control do we then take that name and add it to our existing list of names. We then set index to the last item in the popup menu (so that the new name, if exists, is selected). I found that setting focus (using uicontrol) a second time was necessary to ensure that focus remains with the popup menu.
Try the above and see what happens! (And see the attached for an example.)

More Answers (0)

Community Treasure Hunt

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

Start Hunting!