-1

I want to count all the mouseclicks that are made in a window. I want the counter to increase on every single object i click on, even if its a button, the form it self or a textbox etc. etc.

I have this so far but I cant seem to get it to work:

int mouseCounter = 0;
private void Form1_Load(object sender, EventArgs e)
{
    foreach (Control c in this.Controls)
    {
        c.Click += ClickCounter;
    }
}

void ClickCounter(object sender, EventArgs e)
{
    mouseCounter++;
    label8.Text = mouseCounter.ToString();
}

The counter only respond to click on the controls for now and not the form it self. How can I simply fix that?

Balagurunathan Marimuthu
  • 2,927
  • 4
  • 31
  • 44
grimsan55
  • 265
  • 3
  • 4
  • 20
  • 5
    you'll probably have to use a recursive function to get all controls present on form (`this.Controls` is too restrictive, only "first level controls" will be retrieved) : see http://stackoverflow.com/questions/2525062/how-can-i-query-all-childcontrols-of-a-winform-recursively for example – Raphaël Althaus Oct 23 '13 at 10:33
  • I just started a new project where i only put in this code, a few buttons, few textboxes and a panel. Now the counter responds to every click on every element but doesnt increase when i click on the form it self. So now its the total opposite behaivour from before but the exact same code. – grimsan55 Oct 23 '13 at 10:46
  • Well, in your new project, it certainly won't work if you add a button (or any other control) on the panel... I think that in your "old" project, you have a panel on the "whole form", or something like that (and the other controls are children of this Panel/Control, so this Panel is the only one responding to the click event. In your new project, if you don't have a "main panel", you should also add something like `this.Click +=ClickCounter` – Raphaël Althaus Oct 23 '13 at 10:54
  • yeah you are right, i had groupboxes that i clicked in the first time. The counter do respond to all buttons or all textboxes or every other control i add to the form. So the issue is then that no mouse clicks are registered when i click on the form it self. – grimsan55 Oct 23 '13 at 11:06

3 Answers3

0

You have to use some Application-Wide Click message with an IMessageFilter like this:

public partial class Form1 : Form, IMessageFilter 
{
    int mouseCounter;

    public Form1()
    {
        InitializeComponent();
        Application.AddMessageFilter(this);
    }

    public bool PreFilterMessage(ref Message msg)
    {
        if(msg.Msg == 0x202) //WM_LBUTTONUP 
        {            
            mouseCounter++;
            label8.Text = mouseCounter.ToString();
        }

        return false;
    }
}
Jannick Breunis
  • 195
  • 2
  • 14
King King
  • 61,710
  • 16
  • 105
  • 130
  • @grimsan55 you should add some code declaring `mouseCounter = 0`, and also you should talk a little more about **how it didn't work**? – King King Oct 23 '13 at 11:21
  • yeah sorry, i noticed the mouseCounter before, but i got your code to work as well so thanks :) – grimsan55 Oct 23 '13 at 11:27
0

You may use a message filter, to filter out mouse clicks on your (main)form.

You basically get the message before it is dispatched to the control and may do whatever you want (in your case: increase a counter). The return value of PreFilterMessage(ref Message m) determines wether the message will be dispatched to the control: false means you didn't filter the message and it will be dispatched.

See the documentation for details.

public partial class Form1 : Form, IMessageFilter
{
    public Form1()
    {
        InitializeComponent();
        label1.Text = "0";

        Application.AddMessageFilter(this);

    }

    public bool PreFilterMessage(ref Message m)
    {
        if (m.Msg == 0x201) //wm_lbuttondown
        {
            label1.Text = "" + (Int32.Parse(label1.Text) + 1);
        }
        return false;
    }
}

Tested with .NET4.0 and a form full of various controls.

KeyNone
  • 8,745
  • 4
  • 34
  • 51
-1

Use this:

namespace TicTacToe
{
  public partial class Form1 : Form
  {
    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Visible = false;
    }
  }
}
zx485
  • 28,498
  • 28
  • 50
  • 59
YA boy
  • 1