locked
Issue with Showdialog() WPF RRS feed

  • Question

  • I'm having a strange issue when using multiple WPF Forms

    when the app starts it shows the main form. I have a button to launch a second form and I then hide the first form see code below

    on form 2 there is a button to launch form 3 all using the showdiaglog() when i close form 3 i would expect form 1 to be hidden until form 2 is closed, but form 2 and 3 show 

    form 2 has the same code as below expect opening form3

     
    
    
                Form2 l = new Form2(res, this)
                {
                    Owner = this
                };           
            
    this.Visibility = Visibility.Hidden;
     l.ShowDialog();
                
                
                this.Visibility = Visibility.Visible;

    Saturday, November 21, 2020 5:13 PM

All replies

  • Try an alternative:

    var l = new Form2( res, this );

    this.Visibility = Visibility.Hidden;

    l.Closed += ( o, a ) => this.Visibility = Visibility.Visible;

    l.Show( );

    Similarly for second form.

    Saturday, November 21, 2020 7:14 PM
  • Hi kkoolkev,

    Thank you for posting here.

    If you add a breakpoint to debug step by step, you will see that when Form3 is closed, the ShowDialog in Form2 and MainForm has been executed, and then the code executes "this.Visibility = Visibility.Visible;" which causes two Forms to appear at the same time.

    Viorel_'s solution is great, the basic idea is similar to the following code but more concise.

            private void button_Click(object sender, RoutedEventArgs e)
            {
                Form3 l = new Form3(res,this)
                {
                    Owner = this
                };
    
                this.Visibility = Visibility.Hidden;
                l.ShowDialog();
            }
    
            private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
            {
                this.Owner.Visibility = Visibility.Visible;
            }

    Subscribe to the Closing or Closed event in Form2 and Form3, and then set the Visibility property of the Owner of the currently closed Form when closing.

    By the way, please ask WPF related questions in Microsoft Q&A, MSDN is about to retire.

    Best Regards,

    Timon


    "Visual C#" forum will be migrating to a new home on Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A ! For more information, please refer to the sticky post.

    Monday, November 23, 2020 2:00 AM
  • Hi,

    Has your issue been resolved?

    If so, please click on the "Mark as answer" option of the reply that solved your question, so that it will help other members to find the solution quickly if they face a similar issue. If you still have questions, please feel free to ask.

    Best Regards,

    Timon


    "Visual C#" forum will be migrating to a new home on Microsoft Q&A ! We invite you to post new questions in the "Developing Universal Windows apps" forum’s new home on Microsoft Q&A ! For more information, please refer to the sticky post.

    Monday, November 30, 2020 8:41 AM