简体   繁体   English

用户选择驱动器后如何关闭表单?

[英]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);
        }
        else
            return "";
    }
}

So i have a WindowsForm with a button. 所以我有一个带有按钮的WindowsForm。 The user presses the button, and the FolderBrowserDialog appears. 用户按下按钮,然后出现FolderBrowserDialog。 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 . FolderSelect返回DialogResult.OK ,您需要调用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. 编辑:由于某种原因, FolderSelect方法是静态的。 You should remove the static so it has a reference to the form. 您应该删除静态内容,以便它具有对表单的引用。

You can just call Close() . 您可以只调用Close()
Additionally you can open your Form by using ShowDialog() instead of just Show() and set a DialogResult before closing it: 另外,您可以使用ShowDialog()而不是Show()打开表单,并在关闭对话框之前设置DialogResult

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

EDIT: 编辑:
And your FolderSelect method should be probably void . 您的FolderSelect方法可能应该是void Better save the result of your FolderSelect dialog to a property. 更好地将FolderSelect对话框的结果保存到属性。

Since FolderSelect is static, you won't have access to the form variables. 由于FolderSelect是静态的,因此您将无权访问表单变量。 So, you have two choices. 因此,您有两种选择。

A) Make FolderSelect an instance method. A)使FolderSelect成为实例方法。 Then you can just do this.Close() Mind you, the this is not necessary, I'm just putting it there for clarity. 然后,您可以执行此操作。Close()请注意,这不是必需的,为了清楚起见,我将其放在此处。

B) Return a boolean from FolderSelect, and if it's true, inside the button click event, call this.Close() B)从FolderSelect返回一个布尔值,如果为true,则在按钮click事件内,调用this.Close()

在FolderSelect之后,放置this.Close();。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM