-1

I'm trying to initiate 2 OpenFileDialog in WinForms with two different buttons with the below code as shown:

OpenFileDialog openFileDialog = new OpenFileDialog();
if (openFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
    string filePath = openFileDialog.FileName;
    textPath.Text = filePath;
}

However, only the first button pops up with the window to select a file, and the second button, when clicked, does not pop up with the window to select a file. Any idea why? Thank you!

edit: I'm not wanting to display two dialogues at the same time, I just want two buttons that gets 2 different paths. The dialog is not showing up for me to select a file after clicking the second button, just after selecting a file using the dialog for the first button (dialog closes after that). Hope this clarifies, thanks!

wohlstad
  • 12,661
  • 10
  • 26
  • 39
  • What would be the purpose for doing this? Just show one, then the other. Or allow multiple file selection in a single dailog. – ProgrammingLlama Aug 17 '22 at 03:23
  • Are you saying that you want to display two dialogues at the same time? That's not happening. The first dialogue is modal, so you can't access the calling form until the dialogue is dismissed. – John Aug 17 '22 at 03:26
  • 1
    `The dialog is not showing up for me to select a file after clicking the second button, just after selecting a file using the dialog for the first button (dialog closes after that).` - but given that explanation, you want to have two dialogs at the same time. Because you are saying button 2 should open a dialog, before you have selected and chosen a file for button 1, which equals to two dialogs open at the same time. You first have to choose a file (or abort/close) from the dialog of button 1 before button 2 can show a new dialog. – Rand Random Aug 17 '22 at 04:25
  • This is what you need https://stackoverflow.com/questions/46577025/how-to-re-use-openfiledialog-method-in-c-sharp-for-multiple-buttons – Aung Aug 17 '22 at 05:30
  • Does this answer your question? [How to re-use OpenFileDialog method in C# for multiple buttons](https://stackoverflow.com/questions/46577025/how-to-re-use-openfiledialog-method-in-c-sharp-for-multiple-buttons) – Charlieface Aug 17 '22 at 11:59

1 Answers1

0

OpenFileDialog is a modal dialog, which blocks the calling method when ShowDialog is called, so the second button click is not possible until the dialog is closed.

There's not a built-in non-modal file open form, but you could always create your own or copy one from the interwebs.

D Stanley
  • 149,601
  • 11
  • 178
  • 240