wxFileDialog
Introduction
The wxFileDialog (wxWidgets: wxFileDialog Class Reference) dialog is the standard file selection dialog.
Example
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.
#ifndef _TUTORIALS_WXWIDGETS_WXFILEDIALOG1FRAME_H_ #define _TUTORIALS_WXWIDGETS_WXFILEDIALOG1FRAME_H_ #include <wx/frame.h> #include <wx/textctrl.h> class WxFileDialog1Frame : public wxFrame { public: WxFileDialog1Frame(const wxString& title); private: void OnOpenFile(wxCommandEvent& evt); private: wxTextCtrl* m_textCtrl; wxDECLARE_EVENT_TABLE(); }; #endif
#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; menuFile->Append(wxID_OPEN); menuBar->Append(menuFile, "&File"); SetMenuBar(menuBar); // 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); 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); } // 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(); m_textCtrl->SetValue(selectedFile); } fileDialog->Destroy(); } // Add the event handler for the menu item // to the event table. wxBEGIN_EVENT_TABLE(WxFileDialog1Frame, wxFrame) EVT_MENU(wxID_OPEN, WxFileDialog1Frame::OnOpenFile) wxEND_EVENT_TABLE()
Figure 1 shows the application when the wxFileDialog dialog is displayed and Figure 2 shows the application once the dialog has been closed.
The rest of the source files don't contain significant changes but are shown here for completeness.
#ifndef _TUTORIALS_WXWIDGETS_WXFILEDIALOG1APP_H_ #define _TUTORIALS_WXWIDGETS_WXFILEDIALOG1APP_H_ #include <wx/app.h> class WxFileDialog1App : public wxApp { public: virtual bool OnInit(); }; #endif
#include "WxFileDialog1App.h" #include "WxFileDialog1Frame.h" wxIMPLEMENT_APP(WxFileDialog1App); bool WxFileDialog1App::OnInit() { WxFileDialog1Frame* frame = new WxFileDialog1Frame("WxFileDialog1"); frame->Show(true); return true; }