Table of Contents



The wxFilePickerCtrl (wxWidgets: wxFilePickerCtrl Class Reference) control implements a control to select a file path. A text box is provided where the path can be entered manually but a dialog can also be launched to select the file.

An example of a wxFilePickerCtrl is shown in Figure 1 below.

Figure 1: A wxFilePickerCtrl widget


The wxFilePickerCtrl control is easy to use. Below we show a simple example of a wxFilePickerCtrl with an event handler that updates the value of a wxTextCtrl control when the selected file is changed. The example 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/PickerControls/WxFilePickerCtrl1.

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

  • A wxTextCtrl and wxFilePickerCtrl have been added to the frame.
  • The OnPathChanged method is the event handler for the wxFilePickerCtrl. It updates the contents of the wxTextCtrl to match the currently selected path.
  • The event table links the wxFilePickerCtrl and its event handler.
 File: PickerControls/WxFilePickerCtrl1/src/WxFilePickerCtrl1Frame.h

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

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

    void OnPathChanged(wxFileDirPickerEvent& evt);

    wxTextCtrl* m_textCtrl;


 File: PickerControls/WxFilePickerCtrl1/src/WxFilePickerCtrl1Frame.cpp
#include "WxFilePickerCtrl1Frame.h"
#include "WindowIDs.h"
#include <wx/panel.h>
#include <wx/stattext.h>
#include <wx/sizer.h>

WxFilePickerCtrl1Frame::WxFilePickerCtrl1Frame(const wxString& title)
    : wxFrame(NULL, wxID_ANY, title), m_textCtrl(0)
    // Create a top-level panel to hold all the contents of the frame
    wxPanel* panel = new= wxPanel(this, wxID_ANY);

    // Create a wxFilePickerCtrl control
    wxFilePickerCtrl* filePickerCtrl = new wxFilePickerCtrl(panel, FilePickerID,
        wxEmptyString, wxFileSelectorPromptStr, wxFileSelectorDefaultWildcardStr,
        wxDefaultPosition, wxSize(350, wxDefaultCoord));

    // We add a wxTextCtrl that will contain the same path as
    // the wxFilePickerCtrl. This is done by using the event
    // generated by the wxFilePickerCtrl when the path changes.
    wxStaticText* staticText = new wxStaticText(panel, wxID_ANY, "Selected Path:");
    m_textCtrl = new wxTextCtrl(panel, wxID_ANY);

    // Set up the sizer for the panel
    wxBoxSizer* panelSizer = new wxBoxSizer(wxVERTICAL);
    panelSizer->Add(filePickerCtrl, 0, wxEXPAND | wxALL, 5);
    panelSizer->Add(staticText, 0, wxEXPAND | wxLEFT, 5);
    panelSizer->Add(m_textCtrl, 0, wxEXPAND | wxALL, 5);

    // 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);

// This event handler will be called when the path
// of the wxFilePickerCtrl has changed
void WxFilePickerCtrl1Frame::OnPathChanged(wxFileDirPickerEvent& evt)
    if (m_textCtrl)

// Add the event handler to the event table. As you can see we use the
// window ID to link the event handler to the wxFilePickerCtrl we created.
wxBEGIN_EVENT_TABLE(WxFilePickerCtrl1Frame, wxFrame)
    EVT_FILEPICKER_CHANGED(FilePickerID, WxFilePickerCtrl1Frame::OnPathChanged)

The application is shown below. Figure 2 shows the application with the wxFilePickerCtrl and Figure 3 shows the dialog that gets launched if the browse button is pressed.

Figure 2: The WxFilePickerCtrl1 Application

Figure 3: The WxFilePickerCtrl1 Browse Dialog

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

 File: PickerControls/WxFilePickerCtrl1/src/WxFilePickerCtrl1App.h

#include <wx/app.h>

class WxFilePickerCtrl1App : public wxApp
    virtual bool OnInit();

 File: PickerControls/WxFilePickerCtrl1/src/WxFilePickerCtrl1App.cpp
#include "WxFilePickerCtrl1App.h"
#include "WxFilePickerCtrl1Frame.h"


bool WxFilePickerCtrl1App::OnInit()
    WxFilePickerCtrl1Frame* frame = new WxFilePickerCtrl1Frame("WxFilePickerCtrl1");
    return true;
 File: PickerControls/WxFilePickerCtrl1/src/WindowIDs.h

#include <wx/defs.h>

const wxWindowID FilePickerID = wxID_HIGHEST + 1;