I need a modal dialog to gather some user input. I then need the same data to be consumed by the application MainFrame.
Usually my Modal Dialog would have a pointer to some DataType able to store what I need, and I'd be passing this object by reference from the MainFrame in order to be able to recover data once the modal dialog is closed by the user.
Is this the best way of passing around data?
It doesn't feel right!
Since you are passing data once the user has closed the dialog (presumably on DialogResult.OK), you can easily do this without having a MainFrame reference.
So say you have a TextBox on your dialog, called userNameTextBox and a button that ends the dialog with the OK result. You can either make the userNameTextBox public (not recommended) or add a property to return the text.
public string UserName
{
get { return userNameTextBox.Text; }
}
And to get this value after the dialog has ended, you just do:
Dialog dialog = new Dialog();
if (dialog.ShowDialog() == DialogResult.OK)
{
string username = dialog.UserName;
}