8

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);
        }
    }
Idanis
  • 1,918
  • 6
  • 38
  • 69
  • there was absolutely no need for you to have included all that code just to ask the question of how to have an input field in a folder browser dialog . At most you should've had 2 lines of code in your question . `FolderBrowserDialog fbd = new FolderBrowserDialog();` and `fbd.ShowDialog();` You could've said you've done that, shown those 2 lines,.. And say you want an input field. – barlop May 03 '16 at 10:40

1 Answers1

0

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.

Aleksei Poliakov
  • 1,322
  • 1
  • 14
  • 27
  • Ok, I forgot that `OpenFileDialog` does have that option - but it chooses only files. I wish to also choose a directory, and `OpenFileDialog` does not do that. I am using Windows 7. – Idanis Feb 04 '13 at 08:27
  • How can I do it with `FolderBrowserDialog`? – Idanis Feb 04 '13 at 09:08
  • 1
    @idanis, I think there is not way to add this functionality to `FolderBrowserDialog` (2d case in my initial response), but if you need this functionality there is no reason to limit yourself with `FolderBrowserDialog` - check [this](http://stackoverflow.com/questions/9227917/how-to-use-open-file-dialog-to-select-a-folder) question for example – Aleksei Poliakov Feb 04 '13 at 14:38