Click here to Skip to main content
15,891,976 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi Everyone!

I wanna reset the MouseEventArgs to always run only once.
If somebody click the button more than once (10 times) then the program run 10x and so on.


My code doesn't work well, because if someone click on the button1 2x or more times it will be runned.
I wanna reset the MouseEventArgs or do other solution.

C#
private void button1_MouseClick(object sender, MouseEventArgs e)
        {

            button1.Enabled = false;
            button1.MouseClick -= button1_MouseClick;
            mc++;


            if (true)
            {
                button1.Enabled = false;
                //button1.Location = new Point(40, 40);
                //Point location = button1.Location;
                //location.X = 0; location.Y = 0;
                System.Threading.Thread.Sleep(2000);
                if (mc == 1)
                {

                    //button1.Location = new Point(67, 191);
                    //location.X = 67; location.Y = 191;
                    mc = 0;
                    button1.Enabled = true;
                    button1.MouseClick += button1_MouseClick;

                    textBox1.AppendText("Click " + e.Clicks + ", Clicks: " + mc + "\n");

                }
            }
        }
Posted
Updated 8-Mar-15 22:28pm
v2
Comments
CHill60 9-Mar-15 5:07am    
MouseEventArgs do not "run" - they are passed into the method each time the button receives a mouse click.
Are you trying to stop the button from being used until your processing has finished? You are very close to achieving that, but the code doesn't make much sense (you reset the counter each time)
Roland-HE-C# 9-Mar-15 7:07am    
Yes, I wanna stop the receiving clicks untill the Thread.Sleep(2000) is in progress.
And if the Thread.Sleep(2000) is runned I wanna start the receiving clicks.
I see this function in a flash program and I wanna a smilar in my project. :))
Jan Bakker 9-Mar-15 6:41am    
Do you want the button to be pressed only once the entire lifetime of the program?
Roland-HE-C# 9-Mar-15 7:08am    
Yes, I wanna stop the receiving clicks untill the Thread.Sleep(2000) is in progress.
And if the Thread.Sleep(2000) is runned I wanna start the receiving clicks.
I see this function in a flash program and I wanna a smilar in my project. :))
Jan Bakker 9-Mar-15 7:11am    
Still not clear, to me.. You say yes, but then you say until..?

C#
public Form1()
    {
        InitializeComponent();
    }

    int numberOfTimesButtonPressed = 0;
    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        Application.DoEvents();

        numberOfTimesButtonPressed++;
        System.Threading.Thread.Sleep(5000);

        Application.DoEvents();
        button1.Enabled = true;
    }

    private void Form1_FormClosing(object sender, FormClosingEventArgs e)
    {
        MessageBox.Show(numberOfTimesButtonPressed.ToString());
    }


This button can now only be pressed once, until the method is finished.
 
Share this answer
 
v2
Comments
CHill60 9-Mar-15 6:20am    
This will experience problems if the OP continues to use Sleep in their method - subsequent clicks will be "stored" up until the button is renabled. OP is correct to de-register the MouseClick event as well as setting Enabled = false (i.e. button1.MouseClick -= button1_MouseClick;)
Jan Bakker 9-Mar-15 6:38am    
You are right :)
Roland-HE-C# 9-Mar-15 7:11am    
I thank you for the code but it doesn't start because the "e.Handled" contain mistake ----
Error 2 'System.Windows.Forms.MouseEventArgs' does not contain a definition for 'Handled' and no extension method 'Handled' accepting a first argument of type 'System.Windows.Forms.MouseEventArgs' could be found (are you missing a using directive or an assembly reference?)
Jan Bakker 9-Mar-15 7:13am    
Yes I know, but you say WindowsApplication. That means it can be WPF or Forms. Now I know you mean WindowForms. e.Handled does not exists in windows forms.
Roland-HE-C# 9-Mar-15 7:21am    
Windows Form Application C#.
And I don't wanna fails in my program.
If I allow all clicks it will be wrong to me.
I was intrigued by this as I expected the clicks to be ignored once you had de-registered the MouseClick event.

However, I got this to work using the Application.DoEvents[^] method
C#
private void button1_MouseClick(object sender, MouseEventArgs e)
{
    button1.Enabled = false;

    mc++;

    // If you are doing some long running stuff consider running it 
    // asynchronously on another thread.
    System.Threading.Thread.Sleep(2000);

    // Note e.Clicks will always show 1
    textBox1.AppendText("Click " + e.Clicks + ", Clicks: " + mc + "\n");

    // Consume any events that have been cached
    Application.DoEvents();
    
    // finally re-enable the button
    button1.Enabled = true;
}

Note there was no need for that if(true) statement and you are also resetting the click count each time
 
Share this answer
 
Comments
Roland-HE-C# 9-Mar-15 8:03am    
Thank you very much! :))

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