FolderBrowserDialog with input field

Idanis picture Idanis · Feb 4, 2013 · Viewed 10.6k times · Source

I'm not sure what to google here in order to explain what I wish to do, so I'll try here: I'm using both OpenFileDialog and FolderBrowserDialog in my code for browsing for files and directories respectively.

When the dialogs open, the user gets only the option of actually browsing the tree of files/directories. However, on trees with many directories and sub directories, the users would like to also have the option to manually implicitly write (or paste) the full path the wish to go to.

How can I implement it in the code?

Here are the two functions which use the dialog boxes:

Using FolderBrowserDialog:

    private void buttonAddDirectory_Click(object sender, EventArgs e)
    {
        this.folderBrowserDialog = new System.Windows.Forms.FolderBrowserDialog();
        folderBrowserDialog.SelectedPath = "C:\\";

        if (folderBrowserDialog.ShowDialog() == DialogResult.OK)
        {
            string selectedPath = folderBrowserDialog.SelectedPath;

            if (!searchForFiles(selectedPath))
            {
                MessageBox.Show("The directory: " + selectedPath + " doesn't contain sequences.", "Error!");
                return;
            }

            testForm.enableNumOfProcesses();
            createNewCommand(runBatchScript, selectedPath, true);
        }
    }

Using OpenFileDialog:

    private void buttonAddFile_Click(object sender, EventArgs e)
    {
        this.openFileDialog = new System.Windows.Forms.OpenFileDialog();
        openFileDialog.InitialDirectory = "C:\\";
        openFileDialog.Filter = "PMD files (*" + sequenceExtenssion + ")|*" + sequenceExtenssion + "|All files (*.*)|*.*";
        if (openFileDialog.ShowDialog() == DialogResult.OK)
        {
            string selectedFile = openFileDialog.FileName;
            if (Path.GetExtension(selectedFile).CompareTo(sequenceExtenssion) != 0)
            {
                MessageBox.Show("The file: " + selectedFile + " is not a sequence file.", "Error!");
                return;
            }
            createNewCommand(batchRunExe, selectedFile, false);
        }
    }

Answer

Aleksei Poliakov picture Aleksei Poliakov · Feb 4, 2013

Depending on OS your user is using this is done differently:

  1. Windows 7, Vista, XP, etc. - you can just type metacommands (like D:) into File name input and this metacommand will be executed. Or you can just put your path into the box at the top (need to click in it to switch from navigation view to input view)

  2. If your are using Mono and some other GUI standard dialogs might not provide this functionality at all, so you have to implement these dialogs yourself.