none
Opening only one window at a time RRS feed

  • Question

  • Hi,

    In my application at starting one window is already open. And there is an icon(Actually Button) when I click on that button again the same window gets open. But even if user clicks that button and the window is already open new instance of that window should not open.

    At a time only once instance of that window should be open and visible to the user.

    How to do that??

    Best Regards,

    Swat.

     


    Swat Shel
    Tuesday, January 10, 2012 10:52 AM

Answers

  • Hi,

    Was it your home work :).

    Any ways here it is.

     public partial class Window1 : Window
       {
          WindowONE win1;
          WindowTWO win2;
          public Window1()
          {
             InitializeComponent();
             CreateWindow1();
             CreateWindow2();
          }
    
          public void Window_Loaded(object sender, RoutedEventArgs e)
          {
             this.ShowWindow1();
             this.ShowWindow2();
          }
    
          public void WinONE_Click(object sender, RoutedEventArgs e)
          {
             this.ShowWindow1();
          }
    
          public void WinTWO_Click(object sender, RoutedEventArgs e)
          {
             this.ShowWindow2();
          }
    
          private void OpenUC_Click(object sender, RoutedEventArgs e)
          {
             var ab = new UserControl1();
             ab.OpenWindow1 += (o, args) => { this.ShowWindow1(); };
             ab.OpenWindow2 += (o, args) => { this.ShowWindow2(); };
             canvas1.Children.Add(ab);
          }
    
          private void CreateWindow1()
          {
             win1 = new WindowONE();
             win1.Closed += (o, e) => { win1 = null; };
          }
    
          private void CreateWindow2()
          {
             win2 = new WindowTWO();
             win2.Closed += (o, e) => { win2 = null; };
          }
    
          private void ShowWindow1()
          {
             if (win1 == null) CreateWindow1();
             win1.Show();
             win1.Activate();
          }
    
          private void ShowWindow2()
          {
             if (win2 == null) CreateWindow2();
             win2.Show();
             win2.Activate();
          }
       }
    
    
    public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
            }
    
            public event EventHandler OpenWindow1;
    
            public event EventHandler OpenWindow2;
    
            private void Open_win1(object sender, RoutedEventArgs e)
            {
               if (OpenWindow1 != null) OpenWindow1(this, EventArgs.Empty);
            }
    
            private void Open_win2(object sender, RoutedEventArgs e)
            {
               if (OpenWindow2 != null) OpenWindow2(this, EventArgs.Empty);
            }
        }
    


    Don't call me "sir". hope you get the event call back stuff.

    /Srinivas

    • Proposed as answer by CK Shinde Wednesday, January 11, 2012 12:22 PM
    • Marked as answer by Swat.shel Thursday, January 12, 2012 12:59 PM
    Wednesday, January 11, 2012 10:13 AM

All replies

  • Hi Swati,

    can you place your code?

    my idea is before initialize ?check whether form object is null or not?

     


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Tuesday, January 10, 2012 10:59 AM
  • There's a collection of which forms are open in Application.Openforms.

    So I suggest wherever you're opening your window from, check if it's already in the list and if so do nothing or set focus or whatever your business logic is.

     


    Tuesday, January 10, 2012 11:42 AM
    Moderator
  • Hi Sanz,

    Below is the link of the code,

    https://skydrive.live.com/?cid=fcc8e2da774fc4a3#!/?cid=fcc8e2da774fc4a3&sc=documents&id=FCC8E2DA774FC4A3%21136!cid=FCC8E2DA774FC4A3&id=FCC8E2DA774FC4A3%21136&sc=documents

    In this you will find windowONE and windowTWO are open when window is loaded. and 2 buttons are there when I click on 1, then windowONE should get open. But if windowONE (which is already open at load event) is closed then only windowONE should get open. Otherwise already open windowONE should come to the focus. And same with windowTWO.

    Please help!

    Thanks,

    Swati


    Swat Shel
    Tuesday, January 10, 2012 1:33 PM
  • Hi Andy,

    I am using WPF Window,c#.. what should be done with the window.

    Can you please guide according to that...

     

    Thanks,

    Swat


    Swat Shel
    Tuesday, January 10, 2012 2:26 PM
  • Hi,

    Do you need this?

    public partial class Window1 : Window
        {
           WindowONE win1;
           WindowTWO win2;
    
            public Window1()
            {
                InitializeComponent();
                win1 = new WindowONE();
                win2 = new WindowTWO();
            }
    
            private void Window_Loaded(object sender, RoutedEventArgs e)
            {
                win1.Show();
                win2.Show();
            }
    
            private void WinONE_Click(object sender, RoutedEventArgs e)
            {
                win1.Show();
                win1.Activate();
            }
    
            private void WinTWO_Click(object sender, RoutedEventArgs e)
            {
                win2.Show();
                win2.Activate();
            }
        }
    


    /Srinivas 

    Tuesday, January 10, 2012 3:15 PM
  • Hello Srinivas sir,

    Sir above code is working. But when I am adding one user control, then it is not allowing the same. In the modified version I have added 3rd button on the the window i.e UC1. When I click on that button UserControl1 should get open. There are again the same buttons 1 and 2 on the User Control. But when window1 was initialized at time only we have kept the windowONE and windowTWO open. 

    so when I will click on the 1 in User Control, If windowONE is open it should be only one... Sir please check this new uploaded code.

    https://skydrive.live.com/#cid=FCC8E2DA774FC4A3&id=FCC8E2DA774FC4A3%21136

    How can we do the same for the user Control?

    Thanks,

    swat


    Swat Shel
    Wednesday, January 11, 2012 9:25 AM
  • Hi,

    Was it your home work :).

    Any ways here it is.

     public partial class Window1 : Window
       {
          WindowONE win1;
          WindowTWO win2;
          public Window1()
          {
             InitializeComponent();
             CreateWindow1();
             CreateWindow2();
          }
    
          public void Window_Loaded(object sender, RoutedEventArgs e)
          {
             this.ShowWindow1();
             this.ShowWindow2();
          }
    
          public void WinONE_Click(object sender, RoutedEventArgs e)
          {
             this.ShowWindow1();
          }
    
          public void WinTWO_Click(object sender, RoutedEventArgs e)
          {
             this.ShowWindow2();
          }
    
          private void OpenUC_Click(object sender, RoutedEventArgs e)
          {
             var ab = new UserControl1();
             ab.OpenWindow1 += (o, args) => { this.ShowWindow1(); };
             ab.OpenWindow2 += (o, args) => { this.ShowWindow2(); };
             canvas1.Children.Add(ab);
          }
    
          private void CreateWindow1()
          {
             win1 = new WindowONE();
             win1.Closed += (o, e) => { win1 = null; };
          }
    
          private void CreateWindow2()
          {
             win2 = new WindowTWO();
             win2.Closed += (o, e) => { win2 = null; };
          }
    
          private void ShowWindow1()
          {
             if (win1 == null) CreateWindow1();
             win1.Show();
             win1.Activate();
          }
    
          private void ShowWindow2()
          {
             if (win2 == null) CreateWindow2();
             win2.Show();
             win2.Activate();
          }
       }
    
    
    public partial class UserControl1 : UserControl
        {
            public UserControl1()
            {
                InitializeComponent();
            }
    
            public event EventHandler OpenWindow1;
    
            public event EventHandler OpenWindow2;
    
            private void Open_win1(object sender, RoutedEventArgs e)
            {
               if (OpenWindow1 != null) OpenWindow1(this, EventArgs.Empty);
            }
    
            private void Open_win2(object sender, RoutedEventArgs e)
            {
               if (OpenWindow2 != null) OpenWindow2(this, EventArgs.Empty);
            }
        }
    


    Don't call me "sir". hope you get the event call back stuff.

    /Srinivas

    • Proposed as answer by CK Shinde Wednesday, January 11, 2012 12:22 PM
    • Marked as answer by Swat.shel Thursday, January 12, 2012 12:59 PM
    Wednesday, January 11, 2012 10:13 AM
  • Hello Swathi,

    What do you want to do exacrly?

    as per your code

    what i understood is

    window1 nad window 2 has to be switched according to button clicks and what do you want to do when i click on 3rd?

     

     


    By Sanz. -- If you find this post helpful then please "Vote as Helpful" and "Mark As Answer".
    Wednesday, January 11, 2012 10:25 AM
  • Hi Srinivas,

    That was part of an assignment. Thanks a lot!!!!! It is working!!!

    Thanks,

    Swati


    Swat Shel
    Thursday, January 12, 2012 1:00 PM