MoreThanChaos
MoreThanChaos

Reputation: 2092

c# How to allow some lower level forms to be acessible despite active form called by ShowDialog() method?

i got application, it shows 3 forms: log window, status window, and option window, option window calls some other forms and some of these forms are required to be called using ShowDialog() to return dialog result value for further decision making.

Using ShowDialog() raises problem, cause form called by that method excluding other forms from being accessible.

So my problem is i would like to be able to make atleast log window accessible no matter how much other forms has been called. Is there a way to make log window to independent form other forms, or could it be taken over by form called as last?

EDIT: I failed to mention that the behaviour provided by ShowDialog() is quite usefull in my app and that i only lack ability to free that one or two forms from being locked. Switching to Show() is not option I'm considering as best while other forms, that are parent to form called by ShowDialog() are required to be still locked.

Upvotes: 0

Views: 139

Answers (2)

Grant Winney
Grant Winney

Reputation: 66509

You'll have to make a choice between the two.

  • Use ShowDialog(), so that your parent form pauses execution, and only resumes when the second form is closed, or

  • Use Show(), so that your parent form continues execution after displaying the second form.

If you want to take some action, or read values from the second form when it's closed, then subscribe to its Closed event before you show it.

Let's assume your second form has a "First Name" TextBox, and a property to return that value:

public string FirstName
{
    get { return yourFirstNameTextBox.Text; }
}

In your first form, you can subscribe to an event to take some action when the event occurs, like this:

var f2 = new SecondForm();
f2.Closed += (s, e) => MessageBox.Show(f2.FirstName);
f2.Show();

Now the user can continue on their way with both forms, and when the second form is closed, a message box will display the value of the "First Name" TextBox.

You'll probably want to do something more meaningful than this. Instead of displaying a message box, you could update a field on the first form, or take some any other action or set of actions that you want.

f2.Closed += (s, e) =>
    {
        MessageBox.Show(f2.FirstName);
        nameLabel.Text = f2.FirstName;
        // another action
        // yet another action
    };

Upvotes: 2

ApceH Hypocrite
ApceH Hypocrite

Reputation: 1113

So I now modeled your situation and found solution:

  1. Use Show() instead ShowDialog() for dialog.
  2. Write some code in the dialog to close it window after press the button (Ok, Cancel etc.). Because auto-closing works with ShowDialog only. But you don't need to set DialogResult manually.
  3. At the Options form subscribe to FormClosed event to get DialogResult. For example:

    dlg.FormClosed += (o, a) => { this.Text = dlg.DialogResult.ToString(); };
    
  4. Handle Activate event of Options form to prevent focusing on parent window when dialog is opened:

    if (dlg != null) dlg.Focus();
    

This solution has some difference with system behaviour of ShowDialog, but it works good.

Upvotes: 0

Related Questions