tags:

views:

225

answers:

5

I have a Dialog with showing some information about an object. I want to close this Dialog to show the same dialog but now with the object's sibling. It is a complex dialog that loads different components depending on the object assigned, I cant just change the reference to another object I tried launching the new one in the Closed event, but the former hasn't disapear from the screen and keeps showing. Also tried a static method that is called whithin the Dialog, passing the same Dialog as a parameter, so it close the dialog 'dialog.Close()' and opens a new one with the new object to show. But still the former one keeps opened behind. Is there a way to accomplish that, closing the first window and opening the second?

( THIS IS The Static approach, the window passed by parameter doesn't close until the new one created is closed)

// From the Dialog try to launch the second one closing this.
private void btnSibling_Click(object sender, EventArgs e)
        {
                SwitchToSibling(this);
        }

private static void SwitchToSibling(SiblingDialog window)
        {
            try
            {
                double id = 0;
                id = window.SelectedSibling();
                if (id != 0)
                {
                    // Get's the same Parent so to the new Dialog
                    Control owner = window.Owner;
                    window.Close();
                    Sibling sibling= Sibling.Get(id);
                    SiblingDialog.ShowSibling(sibling, false, owner);
                }
            }
            catch (GroupException ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
A: 

Without knowing the specifics inside the Sibling and SiblingDialog classes, sure you can. The only constraint would be that if you close the window that is the application's main window, the application will exit.

You could, for instance, provide a method like so:

private static void CloseAndShow(Form formToClose, Form formToShow)
{
    formToClose.Close();
    formToShow.Show();
}

That would close formToClose and show formToShow.

Fredrik Mörk
That's pretty much the same I made in my static method, I'm closing one Dialog and showing the new one. As I'm calling this method inside the first Dialog, it doesn't close when the method is executed, but when the function (where the Close() method is called) ends. Seems like the Dispose or other method to release resources is only called after the end of the method.
jmayor
A: 

If you've set the owner for the first dialog, and you know its type, you should be able to solve this problem as follows:

  • Write a custom event on the dialog's owner.
  • When the user clicks the close button, invoke the event.
  • Do not close the dialog from the button.

  • Have the owning window close the dialog when the event handler is invoked. Then have the owning dialog show the sibling dialog.

Mike Hofer
A: 

An extension of Fredrik Mork's answer, that addresses the fact the closing form may be the form controlling the exit status of the program.

private static void CloseAndShow(Form formToClose, Form formToShow)
{
    Application.Run(formToShow);
    formToClose.Close();
    formToShow.Show();
}

Application.Run tells the program to exit when the form passed to it closes

phsr
A: 

The only way I Could find a way around is by calling my dialog from a static method( using sort of a singleton Pattern) , then using a static variable to flag when the Dialog needs to be reopened. So when a dialog execution is ended inside the static method it checks if it needs to be reopened. Otherwise I don't see a possible way.

jmayor
A: 

As it seems that you are setting the owner of the dialog, you can try to use owner.BeginInvoke() in the FormClosed() event handler in MyForm class. :

public partial class MyForm : Form
{
    static int count = 0;
    public MyForm()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        this.Close();
    }

    public static void ShowMyDialog(MyForm form, IWin32Window owner)
    {
        count++;
        form.Text = "My ID: " + count;
        form.ShowDialog(owner);
    }

    delegate void MyDel(MyForm form, IWin32Window owner);

    private void MyForm_FormClosed(object sender, FormClosedEventArgs e)
    {
        MyDel del = ShowMyDialog;
        MyForm mySecondForm = new MyForm();
        this.Owner.BeginInvoke(del, mySecondForm, this.Owner);
    }
}
Chansik Im