Reputation: 1484
I am trying to have a C# program running in the background on Windows that will print "Hello!" after seeing that the user has clicked his or her mouse 10 times. But not just in the console window, anywhere on the screen.
The following event handler for click-tracking is from msdn.microsoft.com:
private void OnMouseDownClickCount(object sender, MouseButtonEventArgs e) {
// Checks the number of clicks.
if (e.ClickCount == 1) {
// Single Click occurred.
lblClickCount.Content = "Single Click";
}
if (e.ClickCount == 2) {
// Double Click occurred.
lblClickCount.Content = "Double Click";
}
if (e.ClickCount >= 3) {
// Triple Click occurred.
lblClickCount.Content = "Triple Click";
}
}
But, I'm not sure how to actually use this. When I add this function anywhere, the MouseButtonEventArgs
type is undefined.
What "using" statements do I need? How do I actually get this code to run properly -- do I call it once from main
? What do I do to call it?
EDIT: Here is a picture showing Visual Studio not understanding MouseButtonEventArgs:
Upvotes: 0
Views: 4106
Reputation: 805
Initially You have to select the form and go to properties, Here you have to go events area and there is MouseClick event. Click that Mouse click. Go to Code behind window. there is the click event generated automatically. In that Form_MouseClick event you can count the number of clicks.
Initially declare a variable
int count = 0;
In method
Private void Form_MouseClick(object sender, MouseEventArgs e)
{
count++;
//add lable which will displays the count value
label.Text=count.ToString();
}
I think which will helps to count the clicks in the form.
Upvotes: 1
Reputation: 5503
I'm not entirely sure what you're trying to accomplish but.. To track user clicks I hooked up the "MouseDown" event on a form in a Windows Forms applications. From there I check click counts in the event handler.
using System;
using System.Windows.Forms;
namespace WindowsFormsApplicationTest
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent( );
this.MouseDown += Form1_MouseDown;
}
private void Form1_MouseDown( object sender, MouseEventArgs e )
{
// Count clicks
}
}
}
Upvotes: 0