[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Detecting a message box being displayed in C#

Posted on 2009-12-28
15
Medium Priority
?
231 Views
Last Modified: 2013-12-17
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?
0
Comment
Question by:kyleboca
  • 9
  • 6
15 Comments
 
LVL 21

Expert Comment

by:silemone
ID: 26132780
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
 
LVL 21

Expert Comment

by:silemone
ID: 26132790
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
 

Author Comment

by:kyleboca
ID: 26133182
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
Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

 
LVL 21

Expert Comment

by:silemone
ID: 26133249
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
 

Author Comment

by:kyleboca
ID: 26133337
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
 
LVL 21

Expert Comment

by:silemone
ID: 26133363
compare it to

 DialogResult.Ok
0
 
LVL 21

Expert Comment

by:silemone
ID: 26133404
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
 
LVL 21

Expert Comment

by:silemone
ID: 26133429
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
 

Author Comment

by:kyleboca
ID: 26133460
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
 
LVL 21

Expert Comment

by:silemone
ID: 26133498
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
 
LVL 21

Accepted Solution

by:
silemone earned 2000 total points
ID: 26133514
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
 

Author Comment

by:kyleboca
ID: 26133537
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
 

Author Closing Comment

by:kyleboca
ID: 31670471
Silemone did a great job.
0
 

Author Comment

by:kyleboca
ID: 26133601
The code compiled fine so I went ahead and accepted your solution.

Thanks again.
0
 
LVL 21

Expert Comment

by:silemone
ID: 26133692
wonderful...

cheers
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Many of us here at EE write code. Many of us write exceptional code; just as many of us write exception-prone code. As we all should know, exceptions are a mechanism for handling errors which are typically out of our control. From database errors, t…
Wouldn’t it be nice if you could test whether an element is contained in an array by using a Contains method just like the one available on List objects? Wouldn’t it be good if you could write code like this? (CODE) In .NET 3.5, this is possible…
Screencast - Getting to Know the Pipeline
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses
Course of the Month19 days, 17 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question