private void locbtn_Click(object sender, EventArgs e)
{
    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Title = "Select Folder";
    openFileDialog1.InitialDirectory = @"C:\";//--"C:\\";
    openFileDialog1.ValidateNames = false;
    openFileDialog1.CheckFileExists = false;
    openFileDialog1.CheckPathExists = true;
    openFileDialog1.ShowDialog();

    if (openFileDialog1.FileName != "")
    {
        textBox1.Text = openFileDialog1.FileName;
    }

    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
    }
    else
    {
        textBox1.Text = "Please Select an Another Folder!";
    }
}

Here is the code. It should display when I click locbtn, but it seems to refuse.

This code was reviewed for 2 days, can you give me an answer? I tried multiple websites' (+ Youtube) source code, and... nothing happens. I tried FolderBrowserDialog also, but no change to nothing.

1

There are 1 best solutions below

0
ChrisBD On

I would rearrange the code so that you can determine what is going on. Your code has two openFileDialog.ShowDialog() statements. It's possible that the return to ShowDialog() is being missed.

Try something like the:

private void locbtn_Click(object sender, EventArgs e)
{
    string fileTextDefault = "Please Select Another Folder!";
    TestBox1.Text = fileTextDefault;

    OpenFileDialog openFileDialog1 = new OpenFileDialog();
    openFileDialog1.Title = "Select Folder";
    openFileDialog1.InitialDirectory = @"C:\";//--"C:\\";
    openFileDialog1.ValidateNames = false;
    openFileDialog1.CheckFileExists = false;
    openFileDialog1.CheckPathExists = true;
    openFileDialog.Filter = "All Files (*.*)|*.*";
    
    if (openFileDialog1.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        var filePath = openFileDialog1.FileName;
        if (filePath != "")
        {
            textBox1.Text = filePath;
        }
    }
    else
    {
      //Do something here to indicate failure
    }
}