MATLAB Creating Graphical User Interfaces

(ff) #1

function listbox1_CreateFcn(hObject, eventdata, handles)
% hObject handle to listbox1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns


% Hint: listbox controls usually have a white background on Windows.
if ispc && isequal(get(hObject,'BackgroundColor'), ...
get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
set(hObject,'String',{'Red';'Green';'Blue'});


The last line, set(hObject,'String',{'Red';'Green';'Blue'}), populates the
contents of the list box.


If you are developing an app programmatically (without GUIDE), then populate the list
box when you create it. For example:


function myui()
figure
uicontrol('Style','Listbox',...
'String',{'Red';'Green';'Blue'},...
'Position',[40 70 80 50]);
end


Change the Selected Item


When the end user selects a list box item, the list box’s Value property changes to a
number that corresponds to the item’s position in the list. For example, a value of 1
corresponds to the first item in the list. If you want to change the selection in your code,
then change the Value property to another number between 1 and the number of items
in the list.


For example, you can use the handles structure in GUIDE to access the list box and
change the Value property:


set(handles.listbox1,'Value',2)


The first argument, handles.listbox1, might be different in your code, depending on
the value of the list box Tag property.


Callbacks for Specific Components
Free download pdf