C# Context menu delaying events - c#

I'm working on an application which needs a right click menu, I've used a contextMenuStrip for the this, but when the menu is open and I rightclick again my form click events are always one behind so the last event is triggered instead of the current one.
I've tried closing the menu when the right mouse button is pressed and showing it when it's released, but it still does the same thing.

I figured out the problem, I was updating the mouse position in an onMouseMove event and keeping it in my static MouseState class and the onMouseMove event wasn't happening while the context menu was up. I'm now updating the mouse on mouse down/up/move using form.PointToClient(Cursor.Position)
Hope this helps others with similar issues.

Related

C# Form2.ShowDialog() and Mouse Cursor

(NET Compact Framework 3.5, Windows CE 6.0)
I want to hide mouse cursor.
So, I use Curosr.Hide()
I have two forms, Form1, Form2.
The size of Form2 is smaller than Form1.
PictureBox1 is in Form1.
When PictureBox1 is clicked, Form2 will be opened. (modal)
At this point, the mouse cursor suddenly appears outside area of Form2.
MouseDonw PictureBox1 -> Form2.ShowDialog -> Show MouseCursor
I have never done Cursor.Show()
Why does the mouse cursor appear?
Added the following
I moved Form2.ShowDialog() from 'MouseDown Event' to 'MouseUp Event'. then it is resolved. Why?
First, form show and other 'actions' usually are done with a mouse click event. That is fired after mouse down and mouse up.
If you break the normal sequence, ie show a form on a mouse down event, the GUI is in 'Mouse down/move' mode, for example to drag an element or draw a line.
As each element can show/hide the mouse cursor, the Windowing System recognizes youre Cursor Hide on the second form, but the first form still shows the mouse cursor as the Mouse Up event is not done.
If you would like to know more about the Basics, you should look at a native WndProc and how are Window Messages are handled. Programming Windows by Charles Petzold is still the bible for Windows programming.

How can I seperate a page click from a drag in WPF?

I need to only fire the page click event if a normal page click happens, not when a user clicks somewhere on my page, then drags the mouse to a different location and lets go of the mouse button. Right now the page click event is fired when the user lets go of the mouse button regardless of where he first clicked or how long the mouse button was held down for. I cannot modify any code in the mouse drag, mouse down, or mouse up events unfortunately, so no solution involving these events will work for me.

NotifyIcon MouseDown MouseEventArgs do not have location data

I have the following event handler for a NotifyIcon within a WPF application using Forms integration:
void MyNotifyIcon_MouseDown(Object sender, System.Windows.Forms.MouseEventArgs e)
{
ShowSettingsWindow();
}
However, e.Location = {X=0,Y=0} always. Is there a way to make this work?
Update
Oddly enough, people have voted to close this question and have downvoted it. However, its clearly not working and simple enough to create a new NotifyIcon inside of a WPF application's App.xaml.cs file and try it out for yourselves.
Update
As Hans pointed out, this is by design. It still doesn't answer the question of what possible workarounds are. Furthermore, this is a bad design, because:
This event occurs on a MouseDown, which means it should have some context of the mouse position when it was clicked in order for it to have occurred. The WmMouseDown handler for NotifyIcon does have the ref Message m property which other controls use to establish the correct position, however it seems to disregard this property and not use it in the event handler. I'd call this a major bug.
Since its post-mortem, getting the Cursor.Position inside of MouseDown will not give you the exact last location in which the MouseDown was raised after the tray icon is clicked. There is a small delay between you clicking it, and it raising the event in which you can further move the mouse. You can test this out yourself with an application that gets the mouse coordinates by quickly moving the mouse after clicking the tray and getting its location within the MouseDown handler. A workaround (which answers my question) would be to save the last location on every MouseMove using Cursor.Position instead and using this last location on MouseDown as the location that clicked the icon. All of which spells out a clear need for the mouse location of when the event actually occurred.
Apparently, I am the only one who cares about these inconsistencies in .NET while a lot of people seem to tolerate them or not understand their implications properly.
Its not ideal because the framework is designed to not send proper mouse event arguments as Hans pointed out, but one workaround is to save the last location on every MouseMove using Cursor.Position and using this last location on MouseDown as the location that contextually clicked the icon.
Have you considered setting a global mouse hook? It would bypass the typical WPF event model, and you would have to manually determine coordinates. This may involve additional API calls.
Details can be found in this post Global mouse event handler

The mouse event GetItemAt(e.X, e.Y) gives wrong values for ListView control

Strange behavior with C# (VS 2010) ListView Control mouse click event (items are without checkbox):
private void m_ListView_MouseClick(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Left)
{
// Returning some times wrong values
ListViewItem lvi = m_ListView.GetItemAt(e.X, e.Y);
//.... more logic here
}
}
After I clicked the item, and immediately I moved the mouse down or up, some other item is getting selected instead of what is intended;
I debugged and came to know that e.X, e.Y is capturing WRONG values. When I click and leave the mouse pointer unmoved, it's working fine. But if I clicked and immediately moved to another row, one of that row is getting selected instead.
Questions:
Is this a known issue with ListView mouse events?
Is there any other way to retrieve correct coordinates for the mouse events?
This is an enterprise level project, where performance is very important. We have written original code for all mouse events like up, down, doubleclick, mouseclick, itemcheck etc.
-Karthik
ListViewItem lvi = m_ListView.FocusedItem;
That should work to get the item that was just clicked (Focused)
m_ListView.GetItemAt(e.X, e.Y);
Would get the item at the mouse location, which is why it changes when you move the mouse quickly away, if you want the item that you just clicked, m_ListView.FocusedItem should work fine
You may want to consider what "wrong value" constitutes. The click-event is a high-level event and the moment the code is executed is not necessarily equal to where the mouse is at that time. The stored coordinates of that click event are from the moment the user clicked the button. If you continue to move the mouse, the coordinates inside the event will be unequal to the coordinates of the mouse at that same moment.
You say that you handle all mouse events. If it is for performance, consider using the build-in mechanisms of the control, these are optimized for speed and accuracy. It's hard to improve on 10 years of development (the control, through various earlier incarnations, has seen quite some development time at MS).
If you really need the utmost speed, write your own control instead from scratch. Use low-level mouse events to get the best available information.
This is by design, the MouseClick event is generated by the MouseUp event. All click events work like that. Pushing the button, moving the mouse and releasing the button thus always gives the location where you released the button, not where you pushed it.
You would need to use the MouseDown event instead.

MouseEnter and MouseLeave Event not raising in user control

Here is the problem:
I have a simple c# form
I have a simple c# user control containing a picturebox and other button.
My form contains one instance of the user control.
I want that when the user do a mouseEnter in the picture box, the mouse cursor change and when the user do a mouseLeave of the picturebox, the mouse go back to normal.
What is happening now is that the events are not fired at all. I put break point into MouseOver, MouseEnter, MouseMove, MouseLeave, etc and none of thems fired. It's the first time I have this problem in C#.
I think it has something to do with the "routed event" but I can't figure it out. If there is another way to achieve what I'm doing, this will also be considered a solution. What is important is that at the end, the user control will be the master of the mouse cursor over his "territory".
Thanks in advance!
What events are you using? The UserController.MouseEnter and UserController.MouseLeave events or the PictureBox.MouseEnter and PictureBox.MouseLeave events?
You should use the latter as the PictureBox will handle the event if the mouse enters the user controller directly through the PictureBox.
As InBetween wrote, PictureBox.MouseXXX should be firing. You can trap those in your UserControl.
If you want the event to be fired on behalf of UserControl, just disable the PictureBox. Be aware though that the event would fire for any mouse position over the UserContrl, not only the PictureBox.

Categories

Resources