mouse cursor doesn't change on MouseLeave event - c#

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.

Related

WPF Grid doesn't fire mouse events when invisible, even though the mouse is capured

In my WPF application I have a Grid with MouseDown, MouseUp and MouseMove events. I want the grid to disappear whenever I press the left mouse button, and reappear when I release it. The problem is that I don't get any mouse events while the grid is invisible (Visibility.Hidden).
This is the MouseDown handler:
private void TabHeaderOnMouseDown(object sender, MouseButtonEventArgs e)
{
tabHeader.CaptureMouse();
tabHeader.Visibility = Visibility.Hidden;
}
And the MouseUp handler:
private void TabHeaderOnMouseUp(object sender, MouseButtonEventArgs e)
{
tabHeader.ReleaseMouseCapture();
tabHeader.Visibility = Visibility.Visible;
}
Setting Opacity to 0 instead of changing the Visibility solved my problem.

Hide cursor over image while using QueryCursor event in WPF

I have an image in my application and I need to draw a line according to the mouse position. What I tried to do is use QueryCursor event to get the mouse position and draw the line, which works as planned.
However, now I wish to hide the cursor while over the image so that only the line is visible. I tried to change the cursor to 'None' but then the event stopped working. What should I do?
You should be able to set the Cursor property to Cursors.None in the QueryCursor event handler and then set it back to null when the MouseLeave event fires.
This works fine for me:
<Image ... QueryCursor="Button_QueryCursor" MouseLeave="Button_MouseLeave" />
private void Image_QueryCursor(object sender, QueryCursorEventArgs e)
{
Cursor = Cursors.None;
//...
}
private void Image_MouseLeave(object sender, MouseEventArgs e)
{
Cursor = null;
}
Please set the Cursors.None to the Windows' Cursor property
i.e this.Cursor = Cursors.None;
Hope this helps.

How to change the cursor shape while dragging over other controls?

I have basically two controls involved in a drag/drop operation. I do this for the start control:
private void controlA_MouseMove(object sender, MouseEventsArgs e)
{
if(e.LeftButton == MouseButtonState.Pressed)
{
//set DataObject...datao
DragDrop.DoDragDrop(controlB, datao, DragDropEffects.Copy | DragDropEffects.Copy;
}
}
The user moves off of controlA, onto controlB and continues dragging to some point on controlB. I've tried the following in several events with no luck to establish a different cursor from the default arrow with the little box under it:
Mouse.OverrideCursor = Cursors.Hand;
and
Mouse.SetCursor(Cursors.Hand);
In these events for controlB, which is where the drop happens:
DragOver
DragEnter
GiveFeedback
How do I get rid of the default arrow with the little box under it while dragging over controlB?
Set ControlB.Cursor = Cursors.Whatever; inside your ControB_MouseEnter() event handler.
You may want to limit it under an if(e.LeftButton == MouseButtonState.Pressed) condition.
Tested MouseEnter while MouseLeftButton is Pressed:

Rectangle area over panel to catch mouse inputs

c# winforms here. I need to draw an invisible rectangle area over a panel and catch his mouse enter/leave events.
My situation (as for some other suggestions you may have):
I have a media player (the panel), on mouse enter event I make visible a little navigation menu (it's located over the panel). I want to hide the nav menu on mouse leave from the panel. This works but unfortunately also entering the nav menu make it invisible. Many thanks.
On mouse leave, simply see if the current Cursor.Position is contained by your rectangle. For example, using a panel and a label:
public Form1()
{
InitializeComponent();
panel1.MouseEnter += panel1_MouseEnter;
panel1.MouseLeave += common_MouseLeave;
label1.MouseLeave += common_MouseLeave;
}
private void panel1_MouseEnter(object sender, EventArgs e)
{
label1.Visible = true;
}
private void common_MouseLeave(object sender, EventArgs e)
{
Rectangle rc = panel1.RectangleToScreen(panel1.ClientRectangle);
if (!rc.Contains(Cursor.Position))
{
label1.Visible = false;
}
}

C# Hold down mouse event

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.

Categories