sony
sony

Reputation: 1503

Check whether message box is open WPF C#?

I have a WPF application where the window gets small and moved to the side if it gets deactivated. But I dont want this feature to happen if there is a messagebox open on the window. Is there a way we can check if there is any dialog box open in C# code?

Upvotes: 7

Views: 9471

Answers (2)

SvenG
SvenG

Reputation: 5195

Wrap the call to your MessageBox in a static class/method. If this is called set a flag that your MessageBox is open.

Something like this:

  public class MessageBoxWrapper
  {
    public static bool IsOpen {get;set;} 

    // give all arguments you want to have for your MSGBox
    public static void Show(string messageBoxText, string caption)
    {
     IsOpen = true;
     MessageBox.Show(messageBoxText, caption);
     IsOpen = false;
    }
  }

Usage:

MessageBoxWrapper.Show("TEST","TEST");
MessageBoxWrapper.IsOpen

But you have to make sure to always use the Wrapper to call a MessageBox

Upvotes: 15

Iain M Norman
Iain M Norman

Reputation: 2085

Set a flag somewhere when you open a MessageBox. Unset it when the MessageBox is closed.

Check the flag when you handling the deactivation.

If there's a possibility of more than one MessageBox being open at a time then you'll need to give that some thought, otherwise one closing will make it look like there are none open.

Upvotes: 1

Related Questions