tags:

views:

103

answers:

3
+3  Q: 

Counting Clicks C#

Hi guys,

Made a simple app which using a timer, counts the number of mouse clicks on a panel for a given duration... simple enough, all working, except it seems to fail to count quickly enough to register all the mouse clicks?

I am literally incrementing a private int value on the click event of the panel, and showing a message box with the results on tick. Any Ideas? Code below...

Matt.

    public partial class Form1 : Form
    {
    int click = 0;

    public Form1()
    {
        InitializeComponent();
    }

    private void panel1_Click(object sender, EventArgs e)
    {
            click++;      
    }

    private void panel1_Paint(object sender, PaintEventArgs e)
    {

    }

    private void btnReset_Click(object sender, EventArgs e)
    {
        timer1.Stop();
        txtClicks.Text = "";
        txtTime.Text = "";
        click = 0;

    }

    private void btnGo_Click(object sender, EventArgs e)
    {
        click = 0;
        timer1.Interval = int.Parse(txtTime.Text) * 1000;  
        timer1.Start();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        timer1.Stop();
        MessageBox.Show(txtClicks.Text + " seconds up, No of clicks:" + click.ToString());
    }
}
+4  A: 

except it seems to fail to count quickly enough to register all the mouse clicks?

may be you should handle Mouse DoubleClick event as well as Mouse Click?

Arseny
haha could it be so simple?! cheers will give that a shot mate :)
Yoda
nice one, hit the nail on the head
Yoda
+4  A: 

Use the MouseDown Event. That'll handle every time and negate the need to handle both Click and DoubleClick.

GenericTypeTea
+1  A: 

I would put money on it that some of the clicks are coming through so fast that...... they count as a double click.

If you add a double click handler, and increment the counter twice while in that handler, does it produce a more accurate result?

slugster