Cursor keeps resetting to default unless I set it on MouseMove - c#

Have a simple form that has a PictureBox in one location. I want to change the cursor to the Cross cursor when entering that control, and change it back when it leaves.
private void Canvas_MouseEnter(object sender, EventArgs e)
{
this.Canvas.Cursor = Cursors.Cross;
}
private void Canvas_MouseLeave(object sender, EventArgs e)
{
this.Canvas.Cursor = Cursors.Default;
}
This doesn't work. If I look closely, I can see it quickly change on MouseEnter, but it flips right back to the default cursor. I have to add "this.Canvas.Cursor = Cursors.Cross;" to the MouseMove event in order for it to work, but then I can constantly see it flickering back to the default cursor.
What gives? This is the only cursor-related code in my whole application, what would be causing it to reset to the default cursor every time I move the mouse?
Thanks.
EDIT: I am an idiot. Person I am collaborating with on this little app had some cursor code tucked away somewhere else that was causing the problem. Thanks guys.

Why don't you set the cursor for the picturebox?
yourPictureBox.Cursor = Cursors.Cross;

I've tried in a new project from scratch (with just the mouseenter/leave handlers and nothing else) and it works.
Might be something else in your application ?

public Form1()
{
InitializeComponent();
pictureBox1.MouseHover += new EventHandler(PictureBox1_MouseHover);
}
void pictureBox1_MouseHover(object sender, EventArgs e)
{
this.PictureBox1.Cursor = Cursors.Cross;
}
You want to use MouseHover event handler.

Related

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

Cursor.Position resets automatically?

I have a simple method :
private void button1_Click(object sender, EventArgs e)
{
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(200, 200);
}
The cursor changes to the point (200,200) but as soon as the mouse is moved the cursor returns back to the original point (where the button is).
I will note that I run this on 2 different computers, on one of them it is working fine, on the other the above problem occurs.
Any suggestions ?
This might be due to a feature called Snap To and can be enabled or disabled from the control panel.
It makes that the mouse moves to a focussed button.
On other option you might try is to set the focus to a different control:
private void button1_Click(object sender, EventArgs e)
{
// I have a pictureBox1 on my form that I could set the focus to
this.pictureBox1.Focus();
System.Windows.Forms.Cursor.Position = new System.Drawing.Point(20, 20);
}

Mouse up event doesn't get triggered

I have the following problem: I have a panel which has a specific color, say red.
When the user presses his mouse, the color of this panel gets stored in a variable. Then the user moves, his mouse still pressed, over to another panel. When he releases the mouse there, this panel should get the background color of the first that had been stored in the variable. My code looks something like this:
public Color currentColor;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
private void AttempsColorChanger(object sender, MouseEventArgs e)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
}
I need to identify the sender first because there are many possible panels that can trigger this event. The first MouseDown method works totally fine, the color is stored nicely in the variable. The secon one however doesn't even get triggered when the user does what I described above. When the ser clicks on the second panel, it works (there is an MouseUp part in a click aswell I guess).
What's wrong here? Why is the event not triggered when the user holds the mouse key down before?
(This answer assumes you are using Windows Forms.)
It could be that you need to capture the mouse by setting this.Capture = true in the MouseDown of the source control. (See Control.Capture)
If you did that, the source window would get the MouseUp event, and it would be the source window that had to determine the destination window under the mouse coords. You can do that using Control.GetChildAtPoint() (see this answer on Stack Overflow).
Use Windows Forms Drag and Drop Support Instead! <- Click for more info
I'm going to suggest you bite the bullet and use the .Net Drag and Drop methods to do this. It requires some reading up, but it will be much better to use it.
You start a drag in response to a MouseDown event by calling Control.DoDragDrop().
Then you need to handle the Control.DragDrop event in the drop target control.
There's a few more things you might need to do to set it up; see the Control.DoDragDrop() documentation for an example.
(For WPF drag and drop support, see here.)
when your mouse enter the target control , mouse down triggerd ang get target BackColor! you need add an boolean flag to your code :
public Color currentColor;
bool flag=false;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
if(flag==false)
{
flag=true
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
}
//assume mouse up for panles
private void AttempsColorChanger(object sender, MouseEventArgs e)
{
if(flag==true)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
flag=flase;
}
}
and also you need change your flag in mouseMove( if )
As I mentioned in my comment Mouse Events are captured by the originating control, You would probably be better off using the DragDrop functionality built into Windows Forms. Something like this should work for you. I assigned common event handlers, so they can be assigned to all of your panels and just work.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void panel_MouseDown(object sender, MouseEventArgs e)
{
((Control)sender).DoDragDrop(((Control)sender).BackColor,DragDropEffects.All);
}
private void panel_DragDrop(object sender, DragEventArgs e)
{
((Control)sender).BackColor = (Color)e.Data.GetData(BackColor.GetType());
}
private void panel_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
}
I know it's an old question but I had the same issue and none of the above answers worked for me. In my case I had to handle the MouseMove event in the target control and check for the mouse to be released. I did set 'BringToFront' on my target panel just in case that helped at all.
public Color currentColor;
private void ColorPickMouseDown(object sender, MouseEventArgs e)
{
Panel pnlSender = (Panel)sender;
currentColor = pnlSender.BackColor;
}
private void panelTarget_MouseMove(object sender, MouseEventArgs e)
{
//the mouse button is released
if (SortMouseLocation == Point.Empty)
{
Panel pnl = (Panel)sender;
pnl.BackColor = currentColor;
}
}

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

WPF equivalent of Control.CursorChanged event

I am trying to change my mouse cursor at certain point when I'm dragging my mouse around in a wpf listview. However, when I set my mouse, it quickly gets overridden by something else, and get changed back to the drag cursor.
I am not sure where the cursor change comes from, it is certainly not from my code, so it has to be system. If it is system, then I have to intercept the event for cursor change, and handle the event in order for the cursor to show what I want right?
So is there a WPF equivalent of this Control.CursorChanged event? Or perhaps there's some other way to approach this problem?
Edit:
here's part of my code
private void SetDragCursor()
{
if (_badDragLoc)
{
Mouse.OverrideCursor = Cursors.No;
}
else
{
Mouse.OverrideCursor = Cursors.Arrow;
}
}
private void listView_DragOver(object sender, DragEventArgs e)
{
if (at a bad drag location)
{
_badDragLoc = true;
SetDragCursor();
}
}
I also have a drag leave event handler, in which I also have the SetDragCursor() method as well. When I step by step go through each line of code in debugger, the mouse turned into the drag cursor from the no cursor right after it enters the drag leave handler. Which is why I think it has to be the system.
If it indeed is the system, then if I can capture the event firing, I can then handle those event myself and not let it bubble through.
Thank you!
Just does not work like that, the way to set the cursor during a DragOver event is the following:
void listView__DragOver(object sender, DragEventArgs e)
{
if (!e.Data.GetDataPresent("Images"))
{
e.Effects = DragDropEffects.None;
e.Handled = true;
}
}
depending on the value of DragDropEffects enum you assign to e.Effects the mouse will change cursor.
do not call Mouse.OverrideCursor because is not the right way.

Categories

Resources