Counting Clicks C# - c#

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());
}
}

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

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?

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?

Related

I need to use a if statement on a button click object within a timer tick?

Basically in the code below where i highlight, i would like to know how to use drawBricks method in the timer tick when that button(btnDisplayBricks) is pressed. Because i am using a timer tick and picturebox for paper drawings etc i cannot just simply call the method from within the button click event because the paper then clears in the timer only allowing me to display the bricks before timer1 starts any ideas.
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (btnDisplayBricks_Click[0] = true) ///code here problem
//then call method
DrawBricks(paper);
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
DrawBricks(paper);
}
}
}
problem is in your equation, you should use == instead of =
if (btnDisplayBricks_Click[0] == true)
also move methodbtnDisplayBricks_Click outside of timer1_Tick
private bool buttonClicked = false;
private void timer1_Tick(object sender, EventArgs e)
{
paper.Clear(Color.LightSteelBlue);
DrawBall();
MoveBall();
DrawBat(paper);
if (buttonClicked)
{
DrawBricks(paper);
// maybe you want to set buttonclicked to false again, but specs are not clear to me
// buttonClicked = false;
}
}
private void btnDisplayBricks_Click(object sender, EventArgs e)
{
DrawBricks(paper);
buttonClicked = true;
}

C# how to get mouse position after INSERT key if pressed, after button click?

I need to know how to get mouse position when I press a key (insert).
This is what I trying to do:
I have a form1 with one buuton, when you press that button it call another form. But before call the form2 i need to get mouse position from an external application. To do this, the user must hover the cursor over requested position and press 'INSERT'.
public partial class _CalibrateGeneralStep2 : Form
{
public _CalibrateGeneralStep2()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button2_Click(object sender, EventArgs e)
{
this.Hide();
///// HERE I NEED TO WAIT UNTIL USER PRESS 'INSERT' KEY BEFORE CALL _CalibrateGeneralStep3 /////
_CalibrateGeneralStep3 frm = new _CalibrateGeneralStep3();
frm.Show();
}
}
I try with keypress and keydown but I dont know use it well.
Thanks... sorry if my english is not good...
You can use
System.Windows.Forms.Cursor.Position: "It represents the current cursor position in screen co-ordinates"
Note: Please refer to the example to see how it works
You can use the KeyDown Event of the form (You can add it from the Designer to be sure it's wired properly)
Since you cannot just wait for the key press event inside your button2_Click, I've used a private field to store the fact that the button have been pressed. Now each time the user press Insert, you check if the button have been pressed and the cursor position. If both are correct, generate the new form.
I've defined the needed cursor position with the 2 constants at the top of the class, and you should also choose a better name for "hasButton2BeenClicked", depending of your business context haha.
public partial class _CalibrateGeneralStep2 : Form
{
private const int NEEDED_X_POSITION = 0;
private const int NEEDED_Y_POSITION = 0;
private bool hasButton2BeenClicked = false;
public _CalibrateGeneralStep2()
{
InitializeComponent();
KeyPreview = true;
}
private void button1_Click(object sender, EventArgs e)
{
Application.Restart();
}
private void button2_Click(object sender, EventArgs e)
{
hasButton2BeenClicked = true;
}
private void OnKeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Insert && IsCursorAtTheCorrectPosition() && hasButton2BeenClicked)
{
GoToNextStep();
}
}
private bool IsCursorAtTheCorrectPosition()
{
return Cursor.Position.X == NEEDED_X_POSITION && Cursor.Position.Y == NEEDED_Y_POSITION;
}
private void GoToNextStep()
{
this.Hide();
new _CalibrateGeneralStep3().Show();
}
}

c# Superposed control doesn't fire events

My Windows Forms code has two superposed PictureBoxes: a small X on the top corner of a user-loaded image. I'll call them DelImage and Image, respectively. Like this:
http://imgur.com/fsW7R8i
The DelImage appears (Visible = true) on Image_MouseEnter and disappears on Image_MouseLeave. Now, I want the DelImage to have a custom behavior as well when the mouse enters it, but DelImage never fires events (nor MouseEnter, nor MouseLeave, nor Click). I've tried bringing it to front, but it already is in front of the Image.
Here's some code:
private void picImage_MouseEnter(object sender, EventArgs e)
{
delImage.Visible = true;
}
private void picImage_MouseLeave(object sender, EventArgs e)
{
delImage.Visible = false;
}
private void picDelImage_MouseEnter(object sender, EventArgs e)
{
delImage.Visible = true;
this.Cursor = Cursors.Hand;
}
private void picDelImage_MouseLeave(object sender, EventArgs e)
{
this.Cursor = Cursors.Arrow;
}
private void picDelImage_Click(object sender, EventArgs e)
{
Panel currentPanel = this.tlpImages.Controls["pnlImage"] as Panel;
currentPanel.Visible = false;
}
With this code, the picDelImage events are never reached. I never enter DelImage, apparently because I never leave Image. What can I do to make my PictureBoxes have the expected behavior?

how to calculate button press time in c#?

There are various buttons in my form and for each button press an action is associated with it.I want to measure the time between button is pressed and released (in millisecs).How can I do it for each button.
In the Form_Load event you can iterate all buttons and dynamically attach Stopwatch to each of them, then handle their MouseDown and MouseUp events:
this.Controls.OfType<Button>().ToList().ForEach(button =>
{
button.Tag = new Stopwatch();
button.MouseDown += new MouseEventHandler(button_MouseDown);
button.MouseUp += new MouseEventHandler(button_MouseUp);
});
And the functions:
void button_MouseUp(object sender, MouseEventArgs e)
{
Stopwatch watch = ((sender as Button).Tag as Stopwatch);
watch.Stop();
MessageBox.Show("This button was clicked for " + watch.Elapsed.TotalMilliseconds + " milliseconds");
watch.Reset();
}
void button_MouseDown(object sender, MouseEventArgs e)
{
((sender as Button).Tag as Stopwatch).Start();
}
Can measure the time span using StopWatch, or use a performance profiler, like
Equatec, which has a free option too.
StopWatch relative StartNew and Stop mthods can inject, in front and at the end of the event handler.
You need to capture the KeyDown and MouseDown for the down event and the KeyUp and MouseUp for the up event.
public Form1()
{
InitializeComponent();
button1.KeyDown += new KeyEventHandler(button1_down);
button1.MouseDown+=new MouseEventHandler(button1_down);
button1.KeyUp += new KeyEventHandler(button1_Up);
button1.MouseUp += new MouseEventHandler(button1_Up);
}
void button1_down(object sender, EventArgs e)
{
Console.WriteLine(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
}
private void button1_Up(object sender, EventArgs e)
{
Console.WriteLine(DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond);
}

How to schedule message popups and show only the last message?

I create following form and I click 3 buttons (randomly). After I click a button, it must show a related message after 3 seconds from clicking it. But if I had clicked another button in the meanwhile, then the related message of that button has to be shown and the previous message in the queue has to be canceled.
As an example, if I click button1 at 11.30.00, then related message should pop up only at 11.30.03 like "You clicked : 1 before 3 Seconds". But if I click another button2 before the 11.30.03, say at 11.30.02 then related message should pop up at 11.30.05 like "You clicked : 2 before 3 Seconds" and the message scheduled for 11.30.03 has to be canceled.
This is my code:
private int signal = 0;
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
signal = 1;
displayMessage(signal);
}
private void button2_Click(object sender, EventArgs e)
{
signal = 2;
displayMessage(signal);
}
private void button3_Click(object sender, EventArgs e)
{
signal = 3;
displayMessage(signal);
}
private void displayMessage(int number)
{
MessageBox.Show("You clicked : "+number+ "before 3 Seconds");
}
Set buttons Tag:
button1.Tag = 1;
button2.Tag = 2;
button3.Tag = 3;
then set Click event:
button1.Click += button_Click;
button2.Click += button_Click;
button3.Click += button_Click;
on the Click event:
private void button_Click(object sender, EventArgs e)
{
signal = (int) ((Button))sender.Tag;
displayMessage(signal);
}
Start a timer. Keep changing signal value according to button click, and when time elapsed is equal to 3 seconds, show up the messagebox from tick event. Set the timer interval according to your need and that will be reflected in the "related message".
int signal = 0;
System.Timers.Timer t = new System.Timers.Timer(3000);
private void Form1_Load(object sender, EventArgs e)
{
//----------------------- other parts of code ---------------------
// at last
t.Elapsed += new System.Timers.ElapsedEventHandler(t_Elapsed);
t.Start();
}
void t_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
{
if (signal == 0)
return;
t.Stop();
MessageBox.Show("You clicked: " + signal + " before " + t.Interval + " Seconds");
signal = 0;
t.Start(); //move this to top of msgbox if you want timer to be reset right after poppin the msgbox.
}
private void button1_Click(object sender, EventArgs e)
{
signal = 1;
t.Stop();
t.Start();
}
private void button2_Click(object sender, EventArgs e)
{
signal = 2;
t.Stop();
t.Start();
}
private void button3_Click(object sender, EventArgs e)
{
signal = 3;
t.Stop();
t.Start();
}
This isn't a place where someone is just going to give you an answer, do some research into the area then post if you get stuck.
you want to create a static timer and check if its finished within each event
http://msdn.microsoft.com/en-us/library/79b3xss3(v=vs.80).aspx
http://msdn.microsoft.com/en-us/library/system.timers.timer(v=vs.71).aspx

Categories

Resources