0

I've created a pop up window in c#. I have a button and when the user clicks the button the window pops up. My problem is that if the user happens to click the button multiple times, the window will be opened multiple times. Once the user has opened the window, I do not want the window to be opened again if the user clicks the button. Is this possible to achieve?

My make the window pop up like this:

PopUp myPopUp = new PopUp(); 
myPopUp.show(); 

I am using wpf.

Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
coder
  • 538
  • 7
  • 17
  • 3
    You haven't even bothered to tell us what GUI API you're using. The obvious answer is, keep a variable that tracks whether the window is already open, and don't open it again. That variable could even just be the reference to the window, making it easy activate. I'm sure the question you want to ask is already answered on Stack Overflow, but the question you posted is so vague, it's not even possible to know which duplicate question is the right one. – Peter Duniho Jul 17 '17 at 06:15
  • I searched the internet and couldn't find an answer to my question. I am new to this so I'm sorry if my question was vague, I'll think of this in my future questions – coder Jul 17 '17 at 06:22
  • also you can disable the button first, then pop up. – Lei Yang Jul 17 '17 at 06:26
  • Possible duplicate of https://stackoverflow.com/questions/12775140/prevent-form-from-showing-multiple-times And https://stackoverflow.com/questions/2018272/preventing-multiple-instance-of-one-form-from-displaying – Shemeer BK Jul 17 '17 at 06:34
  • @ShemeerBK I am using wpf, not forms. – coder Jul 17 '17 at 06:37
  • @Anna , declare the variable in as global and and call the show event when you want in the trigger.its same anyway.! – Shemeer BK Jul 17 '17 at 06:39

3 Answers3

5

If the user needs to access the main window after the popup opened I would create a class member, that holds a reference to the popup and on closing the popup set that reference to null:

PopPp myPopUp = null;

private void OpenPopUp()
{
    if(myPopUp == null)
    {
        myPopUp = new PopUp();
        myPopUp.Closed += (x,y) => { myPopUp = null; };
        myPopUp.Show();
    }
}

If the user does not need to interact with the main window, while the popup is opened just use ShowDialog. It will prevent any input to the main window until the popup is closed:

PopUp myPopUp = new PopUp(); 
myPopUp.ShowDialog(); 
Romano Zumbé
  • 7,893
  • 4
  • 33
  • 55
  • I am using wpf and not forms, so I'm not sure that your first example will work.. – coder Jul 17 '17 at 06:25
  • I've edited my answer to work with WPF. You should always include the UI Framework you use as a tag when asking a question. – Romano Zumbé Jul 17 '17 at 06:28
  • Thank you! It works. I'll think of that when I write my future questions. Guess I was too quick when I was writing this one. – coder Jul 17 '17 at 06:43
2

Best option is to use ShowDialog instead for Show, then the user need to close the popup in order to click the button again. so the code will be :

PopUp myPopUp = new PopUp(); 
myPopUp.ShowDialog(); 

Or else you have to disable the button after first click by using buttonName.Enabled = !buttonName.Enabled; in this case you have to find the point at which button needs to enabled back.

sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
0

Like in-lucky said, the best option is to use ShowDialog. However, if you don't want it to be a modal dialog, you can us Application.Current.Windows which gives you the list of open forms in your applications. Then check if your form is in the list before you show it with your code. Something like this:

if(Application.Current.Windows.OfType<Window>()
   .Where(x => x.Name == "myPopUpName").FirstOrDefault() == null){
  PopUp myPopUp = new PopUp(); 
  myPopUp.show();
}

Alternatively, if you don't want to hardcode the name of the form, you can do it like this:

PopUp myPopUp = new PopUp(); 
if(Application.Current.Windows.OfType<Window>()
   .Where(x => x.Name == myPopUp.Name).FirstOrDefault() == null){
  myPopUp.show();
}

But this wastes some resources because it creates a new copy of the form and then discards it if already open.

Racil Hilan
  • 24,690
  • 13
  • 50
  • 55