wxFilePickerCtrl
Introduction
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.
|
Example
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.
#ifndef _TUTORIALS_WXWIDGETS_WXFILEPICKERCTRL1FRAME_H_
#define _TUTORIALS_WXWIDGETS_WXFILEPICKERCTRL1FRAME_H_
#include <wx/frame.h>
#include <wx/filepicker.h>
#include <wx/textctrl.h>
class WxFilePickerCtrl1Frame : public wxFrame
{
public:
WxFilePickerCtrl1Frame(const wxString& title);
private:
void OnPathChanged(wxFileDirPickerEvent& evt);
private:
wxTextCtrl* m_textCtrl;
wxDECLARE_EVENT_TABLE();
};
#endif
#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->AddSpacer(15);
panelSizer->Add(staticText, 0, wxEXPAND | wxLEFT, 5);
panelSizer->Add(m_textCtrl, 0, wxEXPAND | wxALL, 5);
panel->SetSizer(panelSizer);
// 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);
SetSizerAndFit(topSizer);
}
// This event handler will be called when the path
// of the wxFilePickerCtrl has changed
void WxFilePickerCtrl1Frame::OnPathChanged(wxFileDirPickerEvent& evt)
{
if (m_textCtrl)
{
m_textCtrl->SetValue(evt.GetPath());
}
}
// 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)
wxEND_EVENT_TABLE()
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.
|
|
The rest of the source files don't contain significant changes but are shown here for completeness.
#ifndef _TUTORIALS_WXWIDGETS_WXFILEPICKERCTRL1APP_H_
#define _TUTORIALS_WXWIDGETS_WXFILEPICKERCTRL1APP_H_
#include <wx/app.h>
class WxFilePickerCtrl1App : public wxApp
{
public:
virtual bool OnInit();
};
#endif
#include "WxFilePickerCtrl1App.h"
#include "WxFilePickerCtrl1Frame.h"
wxIMPLEMENT_APP(WxFilePickerCtrl1App);
bool WxFilePickerCtrl1App::OnInit()
{
WxFilePickerCtrl1Frame* frame = new WxFilePickerCtrl1Frame("WxFilePickerCtrl1");
frame->Show(true);
return true;
}
