File-boxes in MFC | CodeGuru

File-boxes in MFC

Wouldn’t it be nice to have drive, dir and file-boxes, as in VisualBasic, in MFC. This project give you just that possibility. To use these list-boxes in your project include FileList.h and FileList.cpp in your project and add the following code: class CLBFileDlg : public CDialog { … public: // Dialog Data //{{AFX_VIRTUAL(CLBFileDlg) protected: … […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 24, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Wouldn’t it be nice to have drive, dir and file-boxes, as in VisualBasic, in MFC.
This project give you just that possibility. To use these list-boxes in your project
include FileList.h and FileList.cpp in your project and add the following code:

class CLBFileDlg : public CDialog
{
    …
public:
    // Dialog Data
//{{AFX_VIRTUAL(CLBFileDlg)	
protected:
    …
    virtual LRESULT WindowProc(UINT message, WPARAM wParam, LPARAM lParam);
    …
    //}}AFX_VIRTUAL
private:
    …
    //the three boxes in this example
    CFileList m_cflDrive;
	CFileList m_cflDir;
    CFileList m_cflFile;
    //user-defined message for communication between the boxes
    //and dialog (msg is trapped in WindowProc)
    UINT m_nBoxMsg;
    …
};
BOOL CLBFileDlg::OnInitDialog()
{
    …
    // TODO: Add extra initialization here	
    //register a personal, user-defined msg so that our boxes can post msg’s for	
    //interaction	
    m_nBoxMsg=RegisterWindowMessage(“Tom’s little box”);
    //init the  boxes	
    //the definition of:	
    //BOOL InitBox(UINT res_from_template, 	
    //	       CWnd *parent, 	
    //	       LPSTR lpszPath, 	
    //	       LPSTR lpszFileMask, 	
    //	       UINT flags, 	
    //	       UINT msg);	
    m_cflDrive.InitBox(IDC_LIST_DRIVE, this, “c:\\”, “*.*”, DRIVES, m_nBoxMsg);
    m_cflDir.InitBox(IDC_LIST_DIR, this, “c:\\”, “*.*”, DIRECTORIES, m_nBoxMsg);
    m_cflFile.InitBox(IDC_LIST_FILE, this, “c:\\”, “*.*”, FILES, m_nBoxMsg);
    //in this example we’ll just display the selected file in a CStatic	
    m_clsSelection.InitSelect(IDC_SELECT, this);
    …
}
LRESULT CLBFileDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
    // TODO: Add your specialized code here and/or call the base class//process the user-defined msg	
        if(message==m_nBoxMsg)
        {
            CString temp_string;
            //update the dir-box and file-box if we change drive
            if(wParam==DRIVES)
            {
                m_cflDir.RefreshBox(m_cflDrive.getPath());
                m_cflFile.RefreshBox(m_cflDrive.getPath());
            }
            //update the file-box if we change directory
            else if(wParam==DIRECTORIES)
            {
                m_cflFile.RefreshBox(m_cflDir.getPath());
            }
            //for simplicity we’ll just show the selected file	
            //in the CStatic	
            else if(wParam==FILES)
            {
                m_cflFile.GetText(m_cflFile.GetCurSel(), temp_string);
                m_clsSelection.SetWindowText(temp_string);
            }
        }
        …
        return CDialog::WindowProc(message, wParam, lParam);
}

Minor update:

Changes since v. 1.0

  • improving update in FillBox(): no update untill the box is filled
  • much faster draw
  • declaring CheckEntry as inline and changing the argument
  • moving FillBox over to private-sector
  • improving OnDblclk():
    if you use the dir-box a lot then the path may look like this
    c:\inn\pics\..\html\..\..\windows\system\..\..\det.log
    this a right and proper path but of course this one can
    be much slimmer, like this:
    c:\det.log

As far as I can see, all the updates are internal to FileList and
does not invalidate usage of this class. However since the header has
had an update, a recompile of the project is required.

Download demo project – 20 KB

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.