In C# we can do something like:
OpenFileDialog dlg = new OpenFileDialog();
if (dlg.ShowDialog() == DialogResult.OK)
{
pbxPhoto.Image = new Bitmap(dlg.OpenFile());
}
But why can we find out what button was pressed without a listener? Does the program flow stop right before the “if” clause till the open file dialog is closed?
The
ShowDialog()method of theOpenFileDialogclass internally handles the button click events, and then returns aDialogResultreturn type indicating which of the buttons was pressed. (So yes, the method is called synchronously, so flow through your calling method does stop until the dialog returns.).You can mimic this behaviour if you write your own modal dialog class (or user control), in which you handle the button click events internally in the dialog class and then provide an enumeration as a result to encapsulate the internal workings of the dialog.
As an alternative you could add your own handler to the FileOk event, but this would probably make more sense using non modal
Show()as per Justin’s answer.