FolderBrowserDialog with input field [duplicate]
Asked Answered
C

1

9

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);
        }
    }
Corespondent answered 4/2, 2013 at 8:11 Comment(1)
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.Dyspepsia
K
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.

Kassi answered 4/2, 2013 at 8:20 Comment(3)
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.Corespondent
How can I do it with FolderBrowserDialog?Corespondent
@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 question for exampleKassi

© 2022 - 2024 — McMap. All rights reserved.