Click here to Skip to main content
15,908,661 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a form that will do a process when a picture box is clicked and then pause three secodnds and return the result. But during this process you can click the picture box and the clicks stack up, thus resulting in it clicking again after 3 seconds. How can I disable the click during the process?

C#
private void DiceBox_Click(object sender, EventArgs e)
        {
            if (doClick)
            {
                return;
            }
            else
            {
                doClick = true;
            }
                int DiceRoll = Dice.DiceRoll(ref DiceBox, ref sound);
                Output.Text = "You rolled: " + DiceRoll + " \r\n";
                //Loads all actions for this session
                Game.LoadActions(Currtable, DiceRoll);
                //Executes Actions.
                Currtable = Game.GoToNextTable();

                Output.Text += TextFetch.ReturnActionText(Currtable, DiceRoll);
                this.Refresh();
                Thread.Sleep(3000);
                //see if checkpoint changes
                if (CheckPoint.Text != Game.GetCheckPoint().ToString())
                {

                    CheckPoint.Text = Game.GetCheckPoint().ToString();

                    //checkpoint sound
                    sound.test(int.Parse(CheckPoint.Text));
                    //map position
                    int maplocationx = map.mapPositionx(int.Parse(CheckPoint.Text));
                    int maplocationy = map.mapPositiony(int.Parse(CheckPoint.Text));
                    MapDot.Location = new Point(maplocationx, maplocationy);

                    CheckPoint.Text += " " + TextFetch.ReturnCheckpointText(int.Parse(CheckPoint.Text));

                }
                doClick = false;
        }


I have tried to us the following code at the beginning of this method and then using += instead of -= at the end but i get the same result as with the above code, the clicks still stack up and wait the thre seconds.
C#
DiceBox.Click -= new System.EventHandler(DiceBox_Click);

and
C#
DiceBox.Click += new System.EventHandler(DiceBox_Click);

How can i fix this issue?

Thanks
Posted
Updated 5-Jan-12 2:18am
v3
Comments
incaunu 5-Jan-12 8:28am    
try to set the button to disable
Sergey Alexandrovich Kryukov 5-Jan-12 12:51pm    
With UI, always tag your application type and UI library you are using: Forms, WPF..? From the context I can see it's Forms, but, say, someone who mostly knows WPF (APS.NET, whatever) would be confused. And you really need to attract an expert in your field, before we open this page.
--SA
bigideaguy 5-Jan-12 13:43pm    
Neither of the suggestions above nor the solution below worked. In the first example I showed I am already introducing a boolean called doClick. I am making a windows forms application by the way.

The picture box is a picture of a die, when you click the picture box the die is rolled, and it scrolls through a number of pictures to simulate this.

While the die is rolling a user can click the picture and it won't have an affect untill the process is done, then the picture box seems to remember the click and thus starting the roll again. If you click 10 times the clicks stack up and the die rolls 10 times and so on.

Any more ideas?

1 solution

Don't make your life harder than it can be, don't use "-=". Instead, add a check of some Boolean flag you can introduce as a member of the declaring class (a Form class, a Window class, whatever); let's say, you name AllowDiceBoxClick.

Add the check in the very beginning of you Click handler:
C#
if (!AllowDiceBoxClick) return;
and you are done!

Just in case:



Review your design to see if you really need PictireBox. Most likely, in your case this is fine, but if you need a bit more complex processing, take into account my past answer: How do I clear a panel from old drawing[^].

—SA
 
Share this answer
 
Comments
Espen Harlinn 5-Jan-12 16:45pm    
Good reply :)
Sergey Alexandrovich Kryukov 5-Jan-12 17:12pm    
Thank you, Espen.
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900