Creating Graphical User Interfaces |
Paging Through the Address Book - Prev/Next
The Prev and Next buttons page back and forth through the entries in the address book. Both push buttons use the same callback, Prev_Next_Callback
. You must set the Callback
property of both push buttons to call this subfunction, as the following illustration of the Prev push button Callback
property setting shows.
Determining Which Button Is Clicked
The callback defines an additional argument, str
, that indicates which button, Prev or Next, was clicked. For the Prev button Callback
property (illustrated above), the Callback
string includes 'Prev'
as the last argument. The Next button Callback
string includes 'Next'
as the last argument. The value of str
is used in case
statements to implement each button's functionality (see the code listing below).
Paging Forward or Backward
Prev_Next_Callback
gets the current index pointer and the addresses from the handles
structure and, depending on which button the user presses, the index pointer is decremented or incremented and the corresponding address and phone number are displayed. The final step stores the new value for the index pointer in the handles
structure and saves the updated structure using guidata
.
Code Listing
function Prev_Next_Callback(hObject, eventdata,handles,str)% Get the index pointer and the addresses
index = handles.Index; Addresses = handles.Addresses;% Depending on whether Prev or Next was clicked change the display
switch str case 'Prev'% Decrease the index by one
i = index - 1;% If the index is less then one then set it equal to the index of the
% last element in the Addresses array
if i < 1 i = length(Addresses); end case 'Next'% Increase the index by one
i = index + 1;% If the index is greater than the size of the array then point
% to the first item in the Addresses array
if i > length(Addresses) i = 1; end end% Get the appropriate data for the index in selected
Current_Name = Addresses(i).Name; Current_Phone = Addresses(i).Phone; set(handles.Contact_Name,'string',Current_Name) set(handles.Contact_Phone,'string',Current_Phone)% Update the index pointer to reflect the new index
handles.Index = i; guidata(hObject, handles)
The Contact Phone Number Callback | Saving Changes to the Address Book from the Menu |