Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check whether message box is open WPF C#?

Tags:

c#

wpf

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?

like image 354
sony Avatar asked Dec 13 '22 06:12

sony


2 Answers

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

like image 86
SvenG Avatar answered Dec 14 '22 22:12

SvenG


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.

like image 29
Iain M Norman Avatar answered Dec 14 '22 20:12

Iain M Norman