The wxDirDialog (wxWidgets: wxDirDialog Class Reference) dialog is the standard directory selection dialog.


The wxDirDialog dialog is easy to use. The example below is a simple modification of the MinimalApp2 example we presented in the minimal application tutorial. The full source for this example is available from our GitHub repository: wxWidgetsTutorials/Dialogs/WxDirDialog1.

In this example a directory selection dialog can be lauched by selecting the "Open..." menu item. When the dialog is closed the selected path is displayed in a wxTextCtrl in the main window.

The source files are shown below. The following modifications were made:

  • We added a wxTextCtrl control to the main frame to display the selected path.
  • We added a menu with an item to launch the wxDirDialog dialog.
  • We added the OnChooseDir event handler for the menu item. It creates the wxDirDialog dialog and when the dialog is closed it sets the value of the wxTextCtrl to the selected path unless the user cancelled the dialog.
 File: Dialogs/WxDirDialog1/src/WxDirDialog1Frame.h

#include <wx/frame.h>
#include <wx/textctrl.h>

class WxDirDialog1Frame : public wxFrame
    WxDirDialog1Frame(const wxString& title);

    void OnChooseDir(wxCommandEvent& evt);

    wxTextCtrl* m_textCtrl;


 File: Dialogs/WxDirDialog1/src/WxDirDialog1Frame.cpp
#include "WxDirDialog1Frame.h"
#include <wx/menu.h>
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/dirdlg.h>

WxDirDialog1Frame::WxDirDialog1Frame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title), m_textCtrl(0)
    // The Open dialog is usually accessible from
    // the "File" menu so we create one.
    wxMenuBar* menuBar = new wxMenuBar;
    wxMenu* menuFile = new wxMenu;
    menuBar->Append(menuFile, "&File");

    // Create a top-level panel to hold all the contents of the frame
    wxPanel* panel = new wxPanel(this, wxID_ANY);

    // Add a wxTextCtrl that will be updated when the
    // user selects a directory
    m_textCtrl = new wxTextCtrl(panel, wxID_ANY,
        L"", wxDefaultPosition, wxSize(400, 200));

    // Set up the sizer for the panel
    wxBoxSizer* panelSizer = new wxBoxSizer(wxHORIZONTAL);
    panelSizer->Add(m_textCtrl, 1, wxEXPAND);

    // Set up the sizer for the frame and resize the frame
    // according to its contents
    wxBoxSizer* topSizer = new wxBoxSizer(wxHORIZONTAL);
    topSizer->Add(panel, 1, wxEXPAND);

// Event handler for the menu item. It will
// display a wxDirDialog.
void WxDirDialog1Frame::OnChooseDir(wxCommandEvent& evt)
    // Create a new wxDirDialog dialog
    wxDirDialog* dirDialog = new wxDirDialog(this);

    // Display the dialog and transfer the contents to
    // the wxTextCtrl on the main frame if the user
    // doesn't cancel
    if (dirDialog->ShowModal() == wxID_OK)
        wxString selectedFile = dirDialog->GetPath();


// Add the event handler for the menu item
// to the event table.
wxBEGIN_EVENT_TABLE(WxDirDialog1Frame, wxFrame)
    EVT_MENU(wxID_OPEN, WxDirDialog1Frame::OnChooseDir)

Figure 1 shows the application when the wxDirDialog dialog is displayed and Figure 2 shows the application once the dialog has been closed.

Figure 1: The WxDirDialog1 application with the wxDirDialog dialog

Figure 2: The WxDirDialog1 application after a path has been selected

The rest of the source files don't contain significant changes but are shown here for completeness.

 File: Dialogs/WxDirDialog1/src/WxDirDialog1App.h

#include <wx/app.h>

class WxDirDialog1App : public wxApp
    virtual bool OnInit();

 File: Dialogs/WxDirDialog1/src/WxDirDialog1App.cpp
#include "WxDirDialog1App.h"
#include "WxDirDialog1Frame.h"


bool WxDirDialog1App::OnInit()
    WxDirDialog1Frame* frame = new WxDirDialog1Frame("WxDirDialog1");
    return true;