Using a Modal Dialog to Confirm an Operation

Modal Dialog Example Outcome

This example illustrates how to use the modal dialog GUI together with another GUI that has a Close button. Clicking the Close button displays the modal dialog, which asks users to confirm that they really want to proceed with the close operation.

The following figure illustrates the dialog positioned over the GUI application, awaiting the user's response.

confirm close  modal dialog box

View Completed Layouts and Their GUI M-Files

If you are reading this in the MATLAB® Help Browser, you can click the following links to display the GUIDE Layout Editor and the MATLAB Editor with a completed version of this example. This enables you to see the values of all component properties and to understand how the components are assembled to create the GUI. You can also see a complete listing of the code that is discussed in the following sections.

Setting Up the Close Confirmation Dialog

To set up the dialog, do the following:

  1. Select New from the File menu in the GUIDE Layout Editor.

  2. In the GUIDE Quick Start dialog, select the Modal Question Dialog template and click OK.

  3. Right-click the static text, Do you want to create a question dialog?, in the Layout Editor and select Property Inspector from the pop-up menu.

  4. Scroll down to String in the Property Inspector and change the String property to Are you sure you want to close?

  5. Select Save from the File menu and type modaldlg.fig in the File name field.

The GUI should now appear as in the following figure.

finished version of the GUI

Setting Up the GUI with the Close Button

To set up the second GUI with a Close button, do the following:

  1. Select New from the File menu in the GUIDE Layout Editor.

  2. In the GUIDE Quick Start dialog, select Blank GUI (Default) and click OK. This opens the blank GUI in a new Layout Editor window.

  3. Drag a push button from the Component palette of the Layout Editor into the layout area.

  4. Right-click the push button and select Property Inspector from the pop-up menu.

  5. Change the String property to Close.

  6. Change the Tag property to close_pushbutton.

  7. Click the M-file Editor icon on the toolbar of the Layout Editor.

  8. Click the Show functions icon on the toolbar of the M-file editor and select close_pushbutton_Callback from the menu.

    The following generated code for the Close button callback should appear in the M-file editor:

    % --- Executes on button press in close_pushbutton.
    function close_pushbutton_Callback(hObject, eventdata, handles)
    % hObject    handle to close_pushbutton (see GCBO)
    % eventdata  reserved - to be defined in a future version of MATLAB
    % handles    structure with handles and user data (see GUIDATA)
  9. After these comments, add the following code:

    % Get the current position of the GUI from the handles structure
    % to pass to the modal dialog.
    pos_size = get(handles.figure1,'Position');
    % Call modaldlg with the argument 'Position'.
    user_response = modaldlg('Title','Confirm Close');
    switch user_response
    case {'No'}
    	% take no action
    case 'Yes'
    	% Prepare to close GUI application window
    	%                  .
    	%                  .
    	%                  .
    	delete(handles.figure1)
    end

Running the GUI with the Close Button

Run the GUI with the Close button by clicking the Run button on the Layout Editor toolbar. The GUI appears as in the following figure:

GUI with close button

When you click the Close button on the GUI, the modal dialog appears as shown in the following figure:

finished modal dialog for the confirm close GUI

Clicking the Yes button closes both the close dialog and the GUI that calls it. Clicking the No button closes just the dialog.

How the GUI and Dialog Work

This section describes what occurs when you click the Close button on the GUI:

  1. User clicks the Close button. Its callback then

  2. The modal dialog opens with the 'Position' obtained from the GUI that calls it.

  3. The opening function in the modal dialog M-file:

  4. When a user clicks one of the two push buttons, the callback for the push button

  5. The output function is called, which returns the string Yes or No as an output argument, and deletes the dialog with the command

    delete(handles.figure1)

  6. When the GUI with the Close button regains control, it receives the string Yes or No. If the answer is 'No', it does nothing. If the answer is 'Yes', the Close button callback closes the GUI with the command

    delete(handles.figure1)

  


 © 1984-2008- The MathWorks, Inc.    -   Site Help   -   Patents   -   Trademarks   -   Privacy Policy   -   Preventing Piracy   -   RSS