Table of Contents



The wxFileDialog (wxWidgets: wxFileDialog Class Reference) dialog is the standard file selection dialog.


The wxFileDialog 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/WxFileDialog1.

In this example a file 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 wxFileDialog dialog.
  • We added the OnOpenFile event handler for the menu item. It creates the wxFileDialog 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/WxFileDialog1/src/WxFileDialog1Frame.h

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

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

    void OnOpenFile(wxCommandEvent& evt);

    wxTextCtrl* m_textCtrl;


 File: Dialogs/WxFileDialog1/src/WxFileDialog1Frame.cpp
#include "WxFileDialog1Frame.h"
#include <wx/menu.h>
#include <wx/panel.h>
#include <wx/sizer.h>
#include <wx/filedlg.h>

WxFileDialog1Frame::WxFileDialog1Frame(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 file
    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 wxFileDialog.
void WxFileDialog1Frame::OnOpenFile(wxCommandEvent& evt)
    // Create a new wxFileDialog dialog
    wxFileDialog* fileDialog = new wxFileDialog(this);

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


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

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

Figure 1: The WxFileDialog1 application with the wxFileDialog dialog

Figure 1: The WxFileDialog1 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/WxFileDialog1/src/WxFileDialog1App.h

#include <wx/app.h>

class WxFileDialog1App : public wxApp
    virtual bool OnInit();

 File: Dialogs/WxFileDialog1/src/WxFileDialog1App.cpp
#include "WxFileDialog1App.h"
#include "WxFileDialog1Frame.h"


bool WxFileDialog1App::OnInit()
    WxFileDialog1Frame* frame = new WxFileDialog1Frame("WxFileDialog1");
    return true;