Customize button run 2 times click event in one single Mouse click - c#

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

Related

How to not confuse the mouse click and mouse down event in windows forms? [duplicate]

This question already has answers here:
Why doesn't doubleclick event fire after mouseDown event on same element fires?
(2 answers)
Closed 5 years ago.
I have a MenuStrip from which I want to drag some things on the form body(then some things happen such as the backcolor of the form is changing, etc). I am handling the MouseDown event, but the thing is that when I click on the option in the ToolStripMenu the same things happen(the backcolor of the form is changing, etc).
What I want is to somehow separate the MouseClick from the MouseDown. More precisely, when I click on one option of the MenuStrip I don't want anything to happen.
When I click, the MouseDown event fires. I want to ignore it unless the mouse's cursor moves.
private void salmonToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
{
//gets the cursor position at the moment when mouse down is activated
p1M = Cursor.Position.X;
p2M = Cursor.Position.Y;
//miscare shows if the mouse moved
if (miscare == true)
{
//do things
}
}
private void Rezervare1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void salmonToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
{
//gets the cursor position to see if it moved
p1m = Cursor.Position.X;
p2m = Cursor.Position.Y;
if (p1M != p1m || p2M != p2m)
{
miscare = true;//it means the cursor moved
}
else miscare = false;
}
If you want your logic to happen on mouse move, then lets handle MouseMove!
bool isMouseDown = false;
private void salmonToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
}
private void salmonToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
private void salmonToolStripMenuItem_MouseMove(object sender, MouseEventArgs e)
{
if(isMouseDown)
{
//Do your thing
}
}
Also note, there are some strange edge cases you can hit with things like the form losing focus, or the user dragging outside of the bounds of the form. Something to be aware of and handle as needed.

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
}

MouseDown/MouseUp and Click events out of sync

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.

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

How do you set cursor on a ToolStripItem

I have some context menu items that are not clickable. They just report the status of something. I don't like how the cursor still appears like they're clickable though.
Anyway to change this?
There isn't a Cursor Field like one would expect.
Handle the MouseMove event of the whole ToolStrip and check if the current mouse location is between the toolStripItem.Bounds. if so, change ToolStrip.Cursor
Amiram sent me in the right direction. You can't set the Cursor on the "ToolStripMenuItem" you have to set it on the parent ContextMenuStrip.
As for the mouse events, that has to go on the ToolStripMenuItems. As the MouseMove event is not fired when the Mouse is over ToolStripMenuItems.
// Init Code
contextMenuStrip1.Cursor = Cursors.Hand;
recentMessagesToolStripMenuItem.MouseLeave += new EventHandler(SetCursorToHandOn_MouseLeave);
recentMessagesToolStripMenuItem.MouseEnter += new EventHandler(SetCursorToArrowOn_MouseEnter);
private void SetCursorToArrowOn_MouseEnter(object sender, EventArgs e)
{
contextMenuStrip1.Cursor = Cursors.Arrow;
}
private void SetCursorToHandOn_MouseLeave(object sender, EventArgs e)
{
contextMenuStrip1.Cursor = Cursors.Hand;
}

Categories

Resources