开发者

How do i close the form after the user picks a drive?

I have this code:

public partial class Form1 : Form
{
    public Form1() { InitializeComponent(); }

    private void button1_Click(object sender, EventArgs e)
    {
        FolderSelect("Please select:");
    }

    public static string FolderSelect(string txtPrompt)
    {
        // Now, we want to use the path information to population
        // our folder selection initial location
        string initialCheckoutPathDir = ("C:\\"); 
        System.IO.DirectoryInfo info =
            new System.IO.DirectoryInfo(initialCheckoutPathDir);

        FolderBrowserDialog FolderSelect = new FolderBrowserDialog();
        FolderSelect.SelectedPath = info.FullName;
        FolderSelect.Description = txtPrompt;
        FolderSelect.ShowNewFolderButton = true;

        if (FolderSelect.ShowDialog() == DialogResult.OK)
        {
            string retPath = FolderSelect.SelectedPath;

            if (retPath == null)
                retPath = "";

            DriveRecursion(retPath);
        开发者_StackOverflow社区}
        else
            return "";
    }
}

So i have a WindowsForm with a button. The user presses the button, and the FolderBrowserDialog appears. Once the user selects a drive, i want the form (with the button) to close as well.

I haven't been having any luck. Any ideas? Syntax would greatly be appreciated.


After the FolderSelect returns DialogResult.OK, you need to call this.close. So like this:

 public string FolderSelect(string txtPrompt)
    {
        //Value to be returned
        string result = string.empty;

        //Now, we want to use the path information to population our folder selection initial location
        string initialCheckoutPathDir = (@"C:\"); 
        System.IO.DirectoryInfo info = new System.IO.DirectoryInfo(initialCheckoutPathDir);
        FolderBrowserDialog FolderSelect = new FolderBrowserDialog();
        FolderSelect.SelectedPath = info.FullName;
        FolderSelect.Description = txtPrompt;
        FolderSelect.ShowNewFolderButton = true;
        if (FolderSelect.ShowDialog() == DialogResult.OK)
        {
            string retPath = FolderSelect.SelectedPath;
            if (retPath == null)
            {
                retPath = "";
            }
            DriveRecursion(retPath);

            result = retPath;
            //Close this form.
            this.Close();
        }
        return result;
    }

Edit: For some reason your FolderSelect method is static. You should remove the static so it has a reference to the form.


You can just call Close().
Additionally you can open your Form by using ShowDialog() instead of just Show() and set a DialogResult before closing it:

DialogResult = FolderSelect.ShowDialog();
Close();

EDIT:
And your FolderSelect method should be probably void. Better save the result of your FolderSelect dialog to a property.


Since FolderSelect is static, you won't have access to the form variables. So, you have two choices.

A) Make FolderSelect an instance method. Then you can just do this.Close() Mind you, the this is not necessary, I'm just putting it there for clarity.

B) Return a boolean from FolderSelect, and if it's true, inside the button click event, call this.Close()


After FolderSelect, put this.Close();

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜