-1
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.

it's me
  • 1
  • 1
  • 2
    What do you mean by 'but it seems to refuse'? The code you've posted shows 2 dialogs, an OpenFileDialog then a FolderBrowserDialog. – HasaniH Jun 05 '23 at 22:45
  • Your folder browser dialog needs an initial folder too. You do not do anything when the user selects a folder, you don't even grab the path – Son of Man Jun 06 '23 at 00:49
  • I mean, I'm not using FolderBrowserDialog, I don't know about it. The `if (diag.ShowDialog() == System.Windows.Forms.DialogResult.OK) { }` was a mistake. – it's me Jun 06 '23 at 12:12
  • @it'sme, unless you post your actual code and elaborate on the problem we can't really help you. Take a look at the guide for [asking questions](https://stackoverflow.com/help/how-to-ask) – HasaniH Jun 06 '23 at 14:24
  • Ok, I edited my question. – it's me Jun 07 '23 at 06:55

1 Answers1

0

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
    }
}
ChrisBD
  • 9,104
  • 3
  • 22
  • 35