Detecting a message box being displayed in C#

I am evaluating the state of some bits in an if statement. If they are true I am displaying a message box using Messagebox.Show(). However there are conditions that may cause the bits to toggle and it causes multiple message boxes to display. Is there a means to prevent another box from displaying if one is already shown?
kylebocaAsked:
Who is Participating?
 
silemoneCommented:
have you tried the code yet?

if that's happening, back to the drawing board...

what's really happening - the effect i was trying to get was:

in my if statement I'm basically calling the messagebox popup, then checking to see if it equals DiaglogResult.ok (ie the ok button has been clicked), if so,then set mbLocked false (if its true that DialogResult.ok is the value then it should be closed since upon pressing ok, the messagebox should close)...otherwise mbLocked stays true...so the next iteration comes and that ok button hasn't been clicked then it doesn't call the messagebox...
0
 
silemoneCommented:
you can place a flag for this...

i.e.
start off with
bool lockMessageBox = false;
when a messagebox instance is created, change to lockMessageBox = true;

place a if statement around any calls for a messagebox and check...

if (lockMessageBox == false)
{
       lockMessageBox =true;
       MessageBox.Show(dafdfd;a);
       lockMessageBox = false;
}
0
 
silemoneCommented:
however if you want to show all of the message boxes one at a time, then place them in a queue
as in this article:

http://www.developerfusion.com/code/4700/a-simple-aspnet-messagebox-class/
0
Cloud Class® Course: Microsoft Azure 2017

Azure has a changed a lot since it was originally introduce by adding new services and features. Do you know everything you need to about Azure? This course will teach you about the Azure App Service, monitoring and application insights, DevOps, and Team Services.

 
kylebocaAuthor Commented:
Hi Silemone. Thanks for your reply.

I don't see how your flag and if statement won't keep iterating the MessageBox.

I attached the code I have now.
if (this.data == 0x00000186)
                    if (current[06] == 0x00 & current[07] == 0x00)
                    {
                        Program.safety._3eStop.BackColor = Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); 
                        MessageBox.Show("An emergency stop button is depressed","Check All E-Stops", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    }
                    else
                        Program.safety._3eStop.BackColor = Color.Green;

Open in new window

0
 
silemoneCommented:
its almost mutex like....it changes the value of the flag...i guess it the value should change flag value back to false after the user clicks the ok button on the dialog...
if (this.data == 0x00000186)
                    if (current[06] == 0x00 & current[07] == 0x00)
                    {
                        Program.safety._3eStop.BackColor = Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); 
                        if (mbLocked == false)
                        {
                              mbLocked == true;  //prevents any other pop ups as long as MessageBox is up...
                              MessageBox.Show("An emergency stop button is depressed","Check All E-Stops", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
 
               //here you should see if the MessageBoxButtons.Ok has been clicked and then if so, 
                              mbLocked == false;
                              
                    }
                    else
                        Program.safety._3eStop.BackColor = Color.Green;

Open in new window

0
 
kylebocaAuthor Commented:
Yes that makes more sense than changing the value back to false in the if statement. I am working on something very similar to your solution right now. I am trying to figure out how to capture the ok button event.

I'm still getting used to OO programming.
0
 
silemoneCommented:
compare it to

 DialogResult.Ok
0
 
silemoneCommented:
I think the code below does the trick...
if (this.data == 0x00000186)
                    if (current[06] == 0x00 & current[07] == 0x00)
                    {
                        Program.safety._3eStop.BackColor = Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); 
                        if (mbLocked == false)
                        {
                              mbLocked == true;  //prevents any other pop ups as long as MessageBox is up...
                              if ((MessageBox.Show("An emergency stop button is depressed","Check All E-Stops", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)== DialogResult.Ok)
                              mbLocked = false;
                        }
                              
                    }
                    else
                        Program.safety._3eStop.BackColor = Color.Green;

Open in new window

0
 
silemoneCommented:
some typos


if (this.data == 0x00000186)
                    if (current[06] == 0x00 & current[07] == 0x00)
                    {
                        Program.safety._3eStop.BackColor = Color.FromArgb(((int)(((byte)(192)))), ((int)(((byte)(0)))), ((int)(((byte)(0))))); 
                        if (mbLocked == false)
                        {
                              mbLocked == true;  //prevents any other pop ups as long as MessageBox is up...
                              if ((MessageBox.Show("An emergency stop button is depressed","Check All E-Stops", MessageBoxButtons.OK, MessageBoxIcon.Exclamation))== DialogResult.Ok)
                              	mbLocked = false;
                        }
                              
                    }
                    else
                        Program.safety._3eStop.BackColor = Color.Green;

Open in new window

0
 
kylebocaAuthor Commented:
I may be missing something obvious but in your code example you are popping the box up and setting mdLocked back to false in the same statement. How will this prevent the next iteration from popping the same box?
0
 
silemoneCommented:
have you tried the code yet?

if that's happening, back to the drawing board...

what's really happening - the effect i was trying to get was:

in my if statement I'm basically calling a popup, then checking to see if it equals DiaglogResult.ok (ie the ok button has been clicked), then set to false...otherwise it stays as true...so the next iteration comes and that ok button hasn't been clicked then it doesn't call the messagebox...
0
 
kylebocaAuthor Commented:
I told you I was missing something obvious. I missed the if statement around your call to the message box. Yes that should work. I have been trying to figure out how to capture the ok button.

Unfortunately I cannot test it unless I am connected to a machine which I will do tomorrow.

Thanks very much for your help. I'll give you the results tomorrow.
0
 
kylebocaAuthor Commented:
Silemone did a great job.
0
 
kylebocaAuthor Commented:
The code compiled fine so I went ahead and accepted your solution.

Thanks again.
0
 
silemoneCommented:
wonderful...

cheers
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.