Frage

Brief: The code (two functions) explaining all what i am trying to do and figures telling my problem

Whenever I have to open a new child Form in my apllication. I use to call the following function. (A child form can have another child)

    public void openNextForm(Form f1, Form f2)
    {
        f2.Owner = f1;                  
        f2.WindowState = FormWindowState.Maximized;
        f2.FormClosing += new FormClosingEventHandler(f_FormClosing);
        f1.Hide();
        f2.ShowDialog();
    }



    // When I close a child form by clicking cross or with ALT-F4
    void f_FormClosing(object sender, FormClosingEventArgs e)
    {
        Form f = sender as Form;
        f.Owner.Show();
    }

On closing child form I use to show the owner/parent form. It is working all right.

But in some cases some controls (Buttons) of owner form are hidden when it is shown on closing child form like

enter image description here

But the actual state of this form wasenter image description here

I have to face this behavior only when I am going back from a complex and lengthy coded child form (still in this case it works fine sometime). Behavior is normal in case of simple (small coded) child forms.

If I press alt key on the weird behaving form, the from is shown surprisingly in the actual state

I have tried to look for this problem and found a lot of similar, but the closest i could see was following and still it is unable to help me at all in case of showdialog

C# Form Problem: new form losing control and randomly hiding

In case of using following code from above link, whenever i open a child form and then again a child form. All forms are hidden. So it is not working as well.

    public void openNextForm(Form f1, Form f2)
    {
        f2.Owner = f1;            
        f2.Show();
        f1.Hide();
        f2.FormClosing += new FormClosingEventHandler(f_FormClosing);
    }

Using tabs should be a solution but i want to keep forms separate and a consistent behavior in all cases during closing/hiding/showing

War es hilfreich?

Lösung

Not sure if it will solve the problem, but i see some improvements that can be made which might do the trick.

Be aware, couldn't verify if it works.

   

public void openNextForm(Form f1, Form f2)
    {
        // we don't need ownership since f1 is hidden.                
        // f2.Owner = f1; 
        f2.WindowState = FormWindowState.Maximized;
        // we don't need this event handled since we use ShowDialog
        //f2.FormClosing += new FormClosingEventHandler(f_FormClosing);
        // The following should hide f1 after f2 is displayed even when using dialog
        f2.Shown += (s, e) => {
            f1.Hide();
        };
        f2.ShowDialog();
        f1.Show();
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top