FunctionX - Practical Learning Logo

File Processing: Using the Save and Open Dialogs

 

Introduction 

The approach we used in our introduction to file processing is appropriate if you don't want the user to participate in saving or opening a file. In many applications, you will leave it up to the users to decide when and where to save a file or when and from where to open a file. This can be done by using the Windows common Dialog boxes designed for file processing.

When a user decides to save or open a file, you can use a CFileDialog variable and call the CFileDialog::GetFileName() method to get the name of the file that the user is dealing with.

Practical Learning: Introducing File Processing

  1. If you want to follow this exercise, open the application created in our introduction and follow in the next section.
    If you didn't create it, then create a new application using MFC AppWizard (exe) or MFC Application
  2. Name it Palace Ice Cream
  3. Change the design of the IDR_MAINFRAME icon as follows:
     
     
  4. Design the dialog box as follows:
     
     
    Control ID Caption Additional Properties
    Static Text Static Text   First Name:  
    Edit Box IDC_FIRST_NAME    
    Static Text Static Text   Last Name:  
    Edit Box Edit Box IDC_LAST_NAME    
    Static Text Static Text   Address:  
    Edit Box IDC_ADDRESS    
    Static Text Static Text   City:  
    Edit Box IDC_CITY    
    Static Text Static Text   State:  
    Edit Box IDC_STATE  
    Static Text Static Text   ZIP Code:  
    Edit Box IDC_ZIP_CODE    
    Static Text Static Text   Hourly Salary:  
    Edit Box IDC_HOURLY_SALARY    
    Group Box   Employment Status  
    Radio Button Radio Button IDC_PART_TIME Part Time Group: True
    Left Text: True
    Radio Button Radio Button IDC_FULL_TIME Full Time Left Text: True
    Button Button IDOK    
    Button Button IDCANCEL    
  5. Using either the ClassWizard or the Add Member Variable Wizard, add the following member variables to the controls:
     
    Identifier Value Variable
    IDC_FIRST_NAME CString m_FirstName
    IDC_LAST_NAME CString m_LastName
    IDC_ADDRESS CString m_Address
    IDC_CITY CString m_City
    IDC_STATE CString m_State
    IDC_ZIP_CODE long m_ZIPCode
    IDC_HOURLY_SALARY double m_HourlySalary
    IDC_PART_TIME int m_EmploymentStatus
  6. Save everything 
 

The Windows Save/Open Common Dialog Boxes

In our introduction, we managed to automatically save or open a file without little intervention from the user. If you want to let the user decide on these issues, you can use the File Dialog.

 

Practical Learning: Using the File Dialog Box

  1. Add two new buttons to the bottom section of the dialog box as follows:
     
      
    Control ID Caption
    Button IDC_SAVE_BTN Save
    Button IDC_OPEN_BTN Open
    Button IDC_RESET_BTN Reset
  2. Save all
  3. Double-click the Save button and accept the suggested name of the file
  4. Double-click the Open button and accept the suggested name of the file
  5. Double-click the Reset button and accept the suggested name of the file
  6. Implement both events as follows:
     
    // Palace Ice CreamDlg.cpp : implementation file
    //
    
    #include "stdafx.h"
    #include "Palace Ice Cream.h"
    #include "Palace Ice CreamDlg.h"
    
    #ifdef _DEBUG
    #define new DEBUG_NEW
    #undef THIS_FILE
    static char THIS_FILE[] = __FILE__;
    #endif
    
    /////////////////////////////////////////////////////////////////////////////
    // CPalaceIceCreamDlg dialog
    
    CPalaceIceCreamDlg::CPalaceIceCreamDlg(CWnd* pParent /*=NULL*/)
    	: CDialog(CPalaceIceCreamDlg::IDD, pParent)
    {
    	//{{AFX_DATA_INIT(CPalaceIceCreamDlg)
    	m_FirstName = _T("");
    	m_LastName = _T("");
    	m_Address = _T("");
    	m_City = _T("");
    	m_State = _T("");
    	m_ZIPCode = 0;
    	m_HourlySalary = 0.0;
    	m_EmploymentStatus = 0;
    	//}}AFX_DATA_INIT
    	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
    }
    
    void CPalaceIceCreamDlg::DoDataExchange(CDataExchange* pDX)
    {
    	CDialog::DoDataExchange(pDX);
    	//{{AFX_DATA_MAP(CPalaceIceCreamDlg)
    	DDX_Text(pDX, IDC_FIRST_NAME, m_FirstName);
    	DDX_Text(pDX, IDC_LAST_NAME, m_LastName);
    	DDX_Text(pDX, IDC_ADDRESS, m_Address);
    	DDX_Text(pDX, IDC_CITY, m_City);
    	DDX_Text(pDX, IDC_STATE, m_State);
    	DDX_Text(pDX, IDC_ZIP_CODE, m_ZIPCode);
    	DDX_Text(pDX, IDC_HOURLY_SALARY, m_HourlySalary);
    	DDX_Radio(pDX, IDC_PART_TIME, m_EmploymentStatus);
    	//}}AFX_DATA_MAP
    }
    
    BEGIN_MESSAGE_MAP(CPalaceIceCreamDlg, CDialog)
    	//{{AFX_MSG_MAP(CPalaceIceCreamDlg)
    	ON_WM_PAINT()
    	ON_WM_QUERYDRAGICON()
    	ON_BN_CLICKED(IDC_SAVE_BTN, OnSaveBtn)
    	ON_BN_CLICKED(IDC_OPEN_BTN, OnOpenBtn)
    	ON_BN_CLICKED(IDC_RESET_BTN, OnResetBtn)
    	//}}AFX_MSG_MAP
    END_MESSAGE_MAP()
    
    /////////////////////////////////////////////////////////////////////////////
    // CPalaceIceCreamDlg message handlers
    
    . . .
    
    
    
    void CPalaceIceCreamDlg::OnSaveBtn() 
    {
    	// TODO: Add your control notification handler code here
    	this->UpdateData();
    
    	CFile flEmployees;
    	char strFilter[] = { "Employees Records (*.mpl)|*.mpl|Palace Ice Cream Files (*.pis)|*.pis|All Files (*.*)|*.*||" };
    
    	CFileDialog FileDlg(FALSE, ".mpl", NULL, 0, strFilter);
    
    	if( FileDlg.DoModal() == IDOK )
    	{
    		if( flEmployees.Open(FileDlg.GetFileName(), CFile::modeCreate | CFile::modeWrite) == FALSE )
    			return;
    		CArchive ar(&flEmployees, CArchive::store);
    
    		ar << m_FirstName << m_LastName << m_Address << m_City << m_State << m_ZIPCode << m_HourlySalary << m_EmploymentStatus;
    		ar.Close();
    	}
    	else
    		return;
    
    	flEmployees.Close();
    }
    
    void CPalaceIceCreamDlg::OnOpenBtn() 
    {
    	// TODO: Add your control notification handler code here
    	CFile flEmployees;
    	char strFilter[] = { "Employees Records (*.mpl)|*.mpl|Palace Ice Cream Files (*.pis)|*.pis|All Files (*.*)|*.*||" };
    
    	CFileDialog FileDlg(TRUE, ".mpl", NULL, 0, strFilter);
    	
    	if( FileDlg.DoModal() == IDOK )
    	{
    		if( flEmployees.Open(FileDlg.GetFileName(), CFile::modeRead) == FALSE )
    			return;
    		CArchive ar(&flEmployees, CArchive::load);
    
    		ar >> m_FirstName >> m_LastName >> m_Address >> m_City >> m_State >> m_ZIPCode >> m_HourlySalary >> m_EmploymentStatus;
    		ar.Close();
    	}
    	else
    		return;
    
    	flEmployees.Close();
    
    	UpdateData(FALSE);
    }
    
    void CPalaceIceCreamDlg::OnResetBtn() 
    {
    	// TODO: Add your control notification handler code here
    	this->m_FirstName.Format("");
    	this->m_LastName.Format("");
    	this->m_Address.Format("");
    	this->m_City.Format("");
    	this->m_State.Format("");
    	this->m_ZIPCode = 0;
    	this->m_HourlySalary = 10.15;
    	this->m_EmploymentStatus = 1;
    
    	this->UpdateData(FALSE);
    }
  7. Test the application
 

Copyright © 2004-2010 FunctionX, Inc.