MouseDown/MouseUp and Click events out of sync - c#

I have a winforms app, that catches MouseDown, MouseUp and Click events.
Clicking the form slowly (once a second or so) and the event counters stay in sync.
Clicking quickly and the down/up event tallys keep track, but click event tally falls behind:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private int clicks = 0;
private void Form1_MouseClick(object sender, MouseEventArgs e)
{
clicks++;
textBox1.Text = clicks.ToString();
}
private int mdown=0;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
mdown++;
textBox2.Text = mdown.ToString();
}
private int mup = 0;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
mup++;
textBox3.Text = mup.ToString();
}
}
Reading docs: https://msdn.microsoft.com/en-us/library/ms171542.aspx this doesn't seem possible - am i missing something obvious
(This happens when using trackpad buttons or external bluetooth mouse, so hopefully this is a programming error and not an issue with the machine.)
EDIT
Damien is of course correct, tracking double clicks as well and everything stays in sync:
private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
clicks++;
textBox1.Text = clicks.ToString();
}

Because sometimes you're clicking fast enough that you're triggering double-clicks. Consider the sequence from the documentation page you linked to:
Following is the order of events raised for a double mouse-button click:
MouseDown event.
Click event.
MouseClick event.
MouseUp event.
MouseDown event.
DoubleClick event. (This can vary, depending on whether the control in question has the StandardDoubleClick style bit set to true. For more information about how to set a ControlStyles bit, see the SetStyle method.)
MouseDoubleClick event.
MouseUp event.
And notice that there are twice as many MouseDown/MouseUp events in that sequence as there are MouseClick events.

Related

Customize button run 2 times click event in one single Mouse click

I have made a customize button that inherited Button class in winform application, and set mouse Event as below
private void _MouseDown(object sender, MouseEventArgs mevent)
{
MState = MouseState.Down;
Invalidate();
}
private void _MouseUp(object sender, MouseEventArgs mevent)
{
MState = MouseState.Up;
Invalidate();
}
private void _MouseMove(object sender, MouseEventArgs mevent)
{
MState = MouseState.Move;
Invalidate();
}
private void _MouseLeave(object sender, EventArgs e)
{
MState = MouseState.Leave;
Invalidate();
}
and in Constructor I set
this.MouseLeave += new EventHandler(_MouseLeave);
this.MouseDown += new MouseEventHandler(_MouseDown);
this.MouseUp += new MouseEventHandler(_MouseUp);
this.MouseMove += new MouseEventHandler(_MouseMove);
But when i implement this button in the form and set Click Event, everytime user click button, it runs Click Event 2 times and in my case 2 record have been inserted into database. I don't know what i was missing , can you help out. Many thanks
This is because when you click the mouse, Mouse Down is fired and then Mouse Up when you release the click.
The MouseDown event of any control is triggered as soon as you click the control,no matter if it's right click/Left click.Although you can target specific moue buttons if you use events such as MouseLeftButtonDown
The MouseUp event fires when you release a control from MouseDown or should i say mouse click.The problem is you are calling the same method/Function(Don't know what Invalidate is) on both the MouseUp and MouseDown event.I hope you understand the issue :)
Without any additional code my guess is that you inherited from button which has its own mouse events and you are adding your own so both are being fired. Your MouseDown and the base class MouseDown. You need to override the native events and not add to them. use the protected override MouseDown() to override them and add your additional code
Many thanks to all of you for the good answer, but by the simple code add to the _MouseDown event my problem is blown up
private void _MouseDown(object sender, MouseEventArgs mevent)
{
MState = MouseState.Down;
(sender as Button).Enabled = false;
(sender as Button).Enabled = true;
(sender as Button).Refresh();
Invalidate();
}

How can I use mousemove event in c#

The question is this:
when the mouse cursor moved on the button some thing should be happen but I don't know what exactly have to write
When you select the button in the VS-designer you will have access to the properties and events (lightning Icon in the property window).
In the events-listing are all events that the button can fire. May be for your purpose the events: ´MouseEnter´ and ´MouseLeave´ would be a good choice. Just double click the event and Visual Studio will generate the appropriate method. Like this:
private void button1_MouseEnter(object sender, EventArgs e)
{
// my code
this.button1.BackColor = Color.Red;
}
private void button1_MouseLeave(object sender, EventArgs e)
{
// my code
this.button1.BackColor = Color.Green;
}
In my example I just change the backcolour of the button when the mouse is on the button and change it again when it leaves the button.
Practically you could run any code inside the generated method.
You can create eventHandler like this :
myButton.MouseMove += new MouseEventHandler(doSomething);
Where myButton is the button from which you want to trigger the event when mouse moves over it. and doSomething() is the method defined as like the following:
public void doSomething(object sender, MouseEventArgs e)
{
// do what ever you want
}

Button mouse over ForeColour change

To create a mouse over button I use this code
private void btnCreateAccount_MouseHover(object sender, EventArgs e)
{
btnCreateAccount.ForeColor = Color.Gold;
}
private void btnCreateAccount_MouseLeave(object sender, EventArgs e)
{
btnCreateAccount.ForeColor = Color.Black;
}
The mouse over button works however when I hover over the button there is a good at least 1 second delay. I would think that it should change colour as soon as the mouse is placed over the button and not with a (in my opinion) too long delay.
Is there any way of fixing that code by like refreshing the button or something along those lines? or perhaps someone has a code that works perfectly?
You are handling the Mouse Hover event. This will require the cursor to be still for a short while in order to fire.
The pause required for this event to be raised is specified in milliseconds by the MouseHoverTime property.
This is read only.
Normally if you want the colour to change immediately you should handle the Mouse Enter event:
private void btnCreateAccount_MouseEnter(object sender, EventArgs e)
{
btnCreateAccount.ForeColor = Color.Gold;
}

event handler to detect mouse drag on picture box(winforms,c#)

I am making simple paint application in which a line would be drawn whenever someone holds down the mouse button and drags(exactly like in windows paint).
However i am having a hard time finding the suitable event handler for this. MouseDown is simply not working and MouseClick is only jotting down dots whenever i press a mouse down.
Need help in this matter.
Thanks.
Handle MouseDown and set a boolean variable to true. Handle MouseMove and, if the variable is set to true and the mouse's movement is above your desired treshold, operate. Handle MouseUp and set that variable to false.
Example:
bool _mousePressed;
private void OnMouseDown(object sender, MouseEventArgs e)
{
_mousePressed = true;
}
private void OnMouseMove(object sender, MouseEventArgs e)
{
if (_mousePressed)
{
//Operate
}
}
private void OnMouseUp(object sender, MouseEventArgs e)
{
_mousePressed = false;
}

WPF event for clicking textboxes in C#

How can I raise an event while clicking a textbox? I'm having trouble finding references for events for WPF in C#.
The idea is to have textboxes fire an event when clicked. For example, let's say as soon as I click a textbox, notepad is executed.
private void textBox1_TextChanged(object sender, TextChangedEventArgs e)
{
hello = Process.Start("notepad");
}
private void Click(object sender, MouseButtonEventArgs e)
{
/* if (e.LeftButton == MouseButtonState.Pressed)
{
hello = Process.Start(#"notepad");
}*/
}
For text events use TextInput event and read entered character from e.Text
private void yourTextBox_TextInput(object sender, TextCompositionEventArgs e)
{
if (e.Text == "K")
{
}
}
for mouse events use MouseDown/MouseUp
Sometimes MouseDown/MouseUp won't work on TextBox, then use this one:
http://msdn.microsoft.com/en-us/library/system.windows.uielement.previewmouseup.aspx
MouseLeftButtonDown event can be raised when clicking on textbox. This event will not fire by default on textbox. We need to use UIElement.AddHandler().
For e.g:
XAML:
<Textbox X:Name="Name_Textbox" MouseLeftButtonDown="Name_Textbox_MouseLeftButtonDown"/>
In the class Constructor:
TextBox_Name.AddHandler(FrameworkElement.MouseLeftButtonDownEvent, new MouseButtonEventHandler("Name_Textbox_MouseLeftButtonDown"), true);
Add Event in class file:
private void Name_Textbox_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
// Your logic on textbox click
}

Categories

Resources