How do you set cursor on a ToolStripItem - c#

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

Related

How to get the mouse coordinate inside an OpenTK.GLControl?

I have a program that uses OpenTk.GLControl. Now on my listener, every time the mouse hovers to the said control, say "glControl1", I want to get the mouse coordinates.
Is that possible? sample code below.
private void glControl1_MouseHover(object sender, EventArgs e)
{
// get the current mouse coordinates
// .........
}
OpenTK.GLControl inherits from System.Windows.Forms.Control. You can use the following code snippet to get the mouse position:
private void glControl1_MouseHover(object sender, EventArgs e)
{
Control control = sender as Control;
Point pt = control.PointToClient(Control.MousePosition);
}
Please refer to the MSDN WinForms documentation for more information.
The problem is that you're using the wrong event. Many UI actions in WinForms trigger multiple events per action; Hover is used for things like popping up tooltips. You don't get a coordinate in Hover because it's unnecessary.
What you want is the MouseMove event. This is used to track the mouse position:
private void glControl1_MouseMove(object sender, MouseEventArgs e)
{
foo = e.X;
bar = e.Y;
}
I dont know, what is OpenTk.GLControl, but:
I was handling swipe events on Windows Phone and did this:
private void PhoneApplicationPage_MouseLeftButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
//string to save coordinates of tap
TapCoordinatesXBegin = e.GetPosition(LayoutRoot).X.ToString();
TapCoordinatesYBegin = e.GetPosition(LayoutRoot).Y.ToString();
}
And i dont remember such event MouseHover - maybe MouseEnter?

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

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

Cursor keeps resetting to default unless I set it on MouseMove

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.

"Unselect" event for MenuItem

I'm trying to implement a 'preview' scenario when the user hover a menu item.
For example, lets say a program has a context-menu with 'Set Color' sub-menu.
The sub-menu pop a list of color to choose from.
Now, when the mouse cursor is over a specific color, I want it to change a label of "Selected color".
And when the mouse cursor leave the selected color menu-item, I want to label to restore its original text.
The following code demonstrate changing the label when menu-item selected - mouse is over.
private void Init()
{
var mnuContextMenu = new ContextMenu();
this.ContextMenu = mnuContextMenu;
var smthingElseMenu = new MenuItem("Do something else");
var setColorMenu = new MenuItem("Set Color");
var colorBlue = new MenuItem("Blue");
var colorRed = new MenuItem("Red");
var colorGreen = new MenuItem("Green");
mnuContextMenu.MenuItems.Add(smthingElseMenu);
mnuContextMenu.MenuItems.Add(setColorMenu);
setColorMenu.MenuItems.Add(colorBlue);
setColorMenu.MenuItems.Add(colorRed);
setColorMenu.MenuItems.Add(colorGreen);
colorBlue.Select += ColorSelect;
colorRed.Select += ColorSelect;
colorGreen.Select += ColorSelect;
}
void ColorSelect(object sender, EventArgs e)
{
lblSelectedColor.Text = ((MenuItem) sender).Text;
}
But I couldn't find a way to make the label text restore when the mouse cursor leave the menu-item.
Any ideas how can I implement some kind of 'Unselect'/'MouseLeave' event for MenuItem?
There's no "un-select" event for MenuItems, unfortunately.
I would just catch the Collapse event of your context menu, and reset your label there. This would have the added benefit that if your user hovers over the "Red" option, then hovers off the context menu, the label should stay red until the context menu closes.
mnuContextMenu.Collapse += (s, e) => lblSelectedColor.Text = "None";
If you really need it to reset the label when your mouse leave the context menu, then you could catch the MouseEnter event of the Panel (or whatever) you have that surrounds the ContextMenu.
MyPanel.MouseEnter += (s, e) => lblSelectedColor.Text = "None";
EDIT Do consider using the ContextMenuStrip class instead. The ToolSTripMenuItem class has a MouseLeave event. And a Checked property, probably what you really want.
Can't you just save the old MenuItem ref.
private MenuItem _oldMenuItem;
void ColorSelect(object sender, EventArgs e)
{
if(_oldMenuItem != null) _oldMenuItem.Text = someText;
_oldMenuItem = sender as MenuItem;
lblSelectedColor.Text = ((MenuItem) sender).Text;
}
Use MouseEnter and MouseLeave events to handle everything. First is raised when when the mouse pointer enters the bounds of this element. And second mouse pointer leaves the bounds - at this point you restore default label text.
EDIT like Hans pointed in comment use ContextMenuStrip and ToolStripMenuItems and youll have those events.
Can't you use:
private void colorBlue_MouseEnter(object sender, EventArgs e)
{
// use color blue
}
private void colorBlue_MouseLeave(object sender, EventArgs e)
{
// use old color
}

Categories

Resources