Select A Directory c++

On occasion a directory needs to be selected programmatically.  The code snippet below offers sound solution.
[codesyntax lang=”cpp”]

std::string bcsGui::SelDir(string initDir)
{
    string selDir;
    wxDirDialog dlg(NULL, "Choose input directory", initDir, wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
    if (dlg.ShowModal() == wxID_OK)
    {
        selDir = dlg.GetPath().ToStdString();
        return selDir;

    }
    dlg.Destroy();

}

[/codesyntax]
Include the target class into the project.
[codesyntax lang=”cpp”]

#include "bcsGui.h"

[/codesyntax]
To successfully gain access to the class the following declaration should be made.
[codesyntax lang=”cpp”]

bcsGui gui;

[/codesyntax]
Now the user may activate the class members by referring to the gui variable.
[codesyntax lang=”cpp”]

    defPath = gui.SelDir("/home/archman/workspace/cb/cpp/");

[/codesyntax]
The variable defPath is described below.
[codesyntax lang=”cpp”]

    string defPath;

[/codesyntax]
To segregate functions by similar operations we have created a new class which is depicted below.
[codesyntax lang=”cpp”]

#include "bcsGui.h"
#include <wx/dirdlg.h>

using namespace std;

bcsGui::bcsGui()
{
    //ctor
}

bcsGui::~bcsGui()
{
    //dtor
}

std::string bcsGui::SelDir(string initDir)
{
    string selDir;
    wxDirDialog dlg(NULL, "Choose input directory", initDir, wxDD_DEFAULT_STYLE | wxDD_DIR_MUST_EXIST);
    if (dlg.ShowModal() == wxID_OK)
    {
        selDir = dlg.GetPath().ToStdString();
        return selDir;

    }
    dlg.Destroy();

}

[/codesyntax]

Mr. Arch Brooks, Software Engineer, Brooks Computing Systems, LLC authored this article.

Leave a Reply

Your email address will not be published. Required fields are marked *