I have a mousemove event that takes the position of the cursor and outputs it to two labels (X and Y), the value dynamically changes as I hover around. I have a mousedown event that when clicked, the same values are outputted to a textbox. How can I combine the mousedown and mousemove events so that when I hover AND hold down the mouse button, the textbox value dynamically changes as I move.
You can interrogate the mouse buttons in your Move event handler, i.e. :
void Form1_MouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) {
String tipText = String.Format("({0}, {1})", e.X, e.Y);
trackTip.Show(tipText, this, e.Location);
}
}
Track the mouse down and mouse up events to set a variable determining whether or not the mouse button is pressed (ie set in down unset in mouse up) then just check this variable in mouse_move
see http://msdn.microsoft.com/en-us/library/system.windows.forms.control.mousebuttons.aspx
for an example
Use
private void OnMouseMove(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
}
}
like this and in second if you will have a condition when your mosue moved and mouse Left button is down.
Related
I have a couple of event handlers attached to a label: one is MouseEnter and the other is MouseLeave. The MouseEnter works fine and changes the mouse cursor to an IBeam as the mouse enters the label boundary, however, the mouse cursor doesn't back to an arrow and remains as an IBeam as the mouse exists the label boundary. I can't seem to figure out what is wrong.
void lbRefLevel_MouseLeave(object sender, MouseEventArgs e)
{
Label lbRefLevel = (Label)sender;
Mouse.OverrideCursor = Cursors.Arrow;
Mouse.Capture(lbRefLevel);
}
void lbRefLevel_MouseEnter(object sender, MouseEventArgs e)
{
Label lbRefLevel = (Label)sender;
Mouse.OverrideCursor = Cursors.IBeam;
Mouse.Capture(lbRefLevel);
}
Set Mouse.OverrideCursor = null; in your mouse leave event, this will reset the override you done on your mouse enter.
Overriding again is not going to help.
I have a form that contains a panel. The idea is that when the mouse is hovered over the panel it displays the OpenHand cursor. You can then drag the panel which causes the cursor to change to the Grab cursor. Upon mouse release the Grab cursor should change back to the OpenHand cursor.
Unfortunately, I'm having an issue that if I drag and release my panel (with the mouse within the bounds of the panel), eventually (usually on the second or third go) the Grab cursor will not change back into the OpenHand cursor.
This remains the case even when you move the mouse out of the panel and back in and try dragging and releasing.
Here is my Panel class:
public class MapPanel : Panel
{
private Point MouseDownLocation;
private Cursor OpenHand = new Cursor(Properties.Resources.hand.Handle);
private Cursor Grab = new Cursor(Properties.Resources.punch.Handle);
public MapPanel()
{
MouseDown += new MouseEventHandler(mapPanel_MouseDown);
MouseUp += new MouseEventHandler(mapPanel_MouseUp);
MouseMove += new MouseEventHandler(mapPanel_MouseMove);
}
private void mapPanel_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Cursor = Grab;
MouseDownLocation = e.Location;
}
}
private void mapPanel_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Cursor = OpenHand;
}
}
private void mapPanel_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
Left = e.X + Left - MouseDownLocation.X;
Top = e.Y + Top - MouseDownLocation.Y;
}
}
}
Interestingly there are no issues if I don't include the MouseMove handler, however the Cursor = OpenHand; statement inside the MouseUp method does still get called with the MouseMove handler included. It just doesn't take effect.
Hopefully someone can shed some light on this. Any help would be very much appreciated!
Mouse left button should be stop select or click when i edit cell in datagridview. i don't know how to disable mouse left button in windows application.
you can manage the action in MouseClick event:
private void dataGridView1_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
// do something
}
}
I need to determine if the value of a NumericUpDown control was changed by a mouseUp event.
I need to call an expensive function when the value of a numericupdown has changed. I can't just use "ValueChanged", I need to use MouseUp and KeyUp events.
Basically, I need to know:
Did the value of the numericUpDown change when the user let go of the
mouse? If any area which is not highlighted in red is clicked, the
answer is no. I need to IGNORE the mouse up event, when ANYWHERE but the red area is clicked.
How can I determine this by code? I find events a little confusing.
This will fire when the user releases the mouse button. You might want to investigate which mousebutton was released.
EDIT
decimal numvalue = 0;
private void numericUpDown1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && numvalue != numericUpDown1.Value)
{
//expensive routines
MessageBox.Show(numericUpDown1.Value.ToString());
}
numvalue = numericUpDown1.Value;
}
EDIT 2
This will determine if the left mousebutton is still down, if it is exit before performing expensive routine, doesn't help with keyboard button down.
private void numericUpDown1_ValueChanged(object sender, EventArgs e)
{
if ((Control.MouseButtons & MouseButtons.Left) == MouseButtons.Left)
{
return;
}
//expensive routines
}
Edit 3
How to detect the currently pressed key?
Will help solve the Any key down, Though I think the only ones that matter are the arrow keys
Problem - I need to IGNORE the mouse up event, when ANYWHERE but the red area is clicked.
Derive a custom numeric control as shown below. Get the TextArea of the Numeric Control and ignore the KeyUp.
class UpDownLabel : NumericUpDown
{
private Label mLabel;
private TextBox mBox;
public UpDownLabel()
{
mBox = this.Controls[1] as TextBox;
mBox.Enabled = false;
mLabel = new Label();
mLabel.Location = mBox.Location;
mLabel.Size = mBox.Size;
this.Controls.Add(mLabel);
mLabel.BringToFront();
mLabel.MouseUp += new MouseEventHandler(mLabel_MouseUp);
}
// ignore the KeyUp event in the textarea
void mLabel_MouseUp(object sender, MouseEventArgs e)
{
return;
}
protected override void UpdateEditText()
{
base.UpdateEditText();
if (mLabel != null) mLabel.Text = mBox.Text;
}
}
In the MainForm, update your designer with this control i.e. UpDownLabel:-
private void numericUpDown1_MouseUp(object sender, MouseEventArgs e)
{
MessageBox.Show("From Up/Down");
}
Referred from - https://stackoverflow.com/a/4059473/763026 & handled the MouseUp event.
Now, use this control instead of the standard one and hook on the
KeyUp event. You will always get the KeyUp event from the Up/Down button only i.e. RED AREA when you click the
spinner [Up/Down button, which is again a different control derived
from UpDownBase].
I think you should use Leave event that when the focus of NumericUpDown control gone, it would called.
int x = 0;
private void numericUpDown1_Leave(object sender, EventArgs e)
{
x++;
label1.Text = x.ToString();
}
If you place a panel in a new C# project and capture it's MouseMove event like this:
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button != MouseButtons.Left)
return;
Console.WriteLine("e.X: {0}, e.Y: {1}", e.X, e.Y);
}
It tells you the client coordinates of the mouse while the left mouse button is held down, even if the cursor goes outside of the panel.
However, if you are holding down left mouse button in the container and then, while holding down left mouse button, click any other mouse button on your mouse, it no longer calls MouseMove while outside the bounds of the container.
Is there any way to change this? Thanks for reading.
1: If you are trying to get it to work only when the left button is down, try the following:
bool mouseDown = false;
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (!mouseDown)
return;
Console.WriteLine("e.X: {0}, e.Y: {1}", e.X, e.Y);
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
mouseDown = true;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
mouseDown = false;
}
2: Otherwise, if you want it to work when any mouse button is down, try the following:
int mouseDown = 0;
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (mouseDown == 0)
return;
Console.WriteLine("e.X: {0}, e.Y: {1}", e.X, e.Y);
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
mouseDown++;
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
mouseDown--;
}
In addition to Justin's solution will say that:
I think it's because, if during drag you go out of the panel right click out of the panel, forces panel to lose the focus, so control no more becomes an active one.
In case of when you're dragging mouse inside panel and click with right click inside panel, happens something like this.
I just captured with Spy++ windows explorer and did a test, so moved the mouse with LButton down and at some point without releasing it made a right click. And here is a result:
With arrows I sign the row where I clicked with right button, where WM_CAPTURECHANGED
message sent. This message according to documentation is:
Sent to the window that is losing the mouse capture.
Look on next line with arrow. The handle of the next window is 0, so there is no any window. So this means, like a simple command: You lost a capture on mouse.
Hope this helps.