MouseMove event prevents the MouseRightButtonUp event in WPF - c#

I have drawn some lines using DrawingVisual. Now I'm trying to implement a simple hit testing.
When the mouse pointer moves over a line I'm redrawing it with twice its thickness to give the user a sense of highlighting (Magenta line in the image below)
This works very well until I also implement a MouseRightButtonUp event. I want to show a messagebox when the user right clicks the line. But somehow while the mouse is over the line and the line is highlighted, the MouseRightButtonUp event does not raise at all. What could be the problem?
MouseMove += OnMouseMove;
MouseRightButtonUp += OnMouseRightButtonUp;
private void OnMouseMove(object sender, MouseEventArgs e)
{
var p = e.GetPosition(Window);
// Check if the mouse hit any lines and redraw
ReDraw();
}
void OnMouseRightButtonUp(object sender, MouseButtonEventArgs e)
{
// Check if any line is hit and show the message box
if (lineHit)
{
MessageBox.Show("You hit the line!");
}
}

Related

How to know when user doesn't click anymore on a button WPF

I'm doing some password eye text box.
I want, that when the user would press on the eye button, the password would be visible and when he leaves the mouse button it would be unvisible again.
My question: How to know when the user stopped clicking the mouse button?
Image of how it looks like:
My code is roughly as follows:
private void eye_click(object sender , RoutedEventArgs e)
{
//here I making the password visible
}
I'm looking for something like:
private void user_doesnt_click_anymore_click(object sender , RoutedEventArgs e)
{
//return the password to be unvisible
}
You can react to different events, depending on your
Form-Example
this.button1.Click += new System.EventHandler(this.button1_Click);
this.button1.MouseClick += new System.Windows.Forms.MouseEventHandler(this.button1_MouseClick);
this.button1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.button1_MouseDown);
this.button1.MouseUp += new System.Windows.Forms.MouseEventHandler(this.button1_MouseUp);
private void button1_Click(object sender, EventArgs e)
{
Console.WriteLine("button1_Click");
}
private void button1_MouseClick(object sender, MouseEventArgs e)
{
Console.WriteLine("button1_MouseClick");
}
private void button1_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("button1_MouseDown");
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
Console.WriteLine("button1_MouseUp");
}
MouseDown
Fires if you mouse is over the button an a mousebutton is pushed down.
First chance to see something
Here you want to show the pw.
MouseUp
Fires if you release the mousebutton which you "downed" on the button.
Fired when stopped clicking. You're not neccessarily on the button anymore!
Here you want to hide the pw.
MouseClick
Fires if a MouseDown AND MouseUp is performed and you're still hovering the button.
This prevents hitting buttons with the keyboard. Perhaps helpful in some situations?!
Click
Fires if the button is clicked (Pushed down and released). This could also be done my selecting the button by tabbing to it and pushing space.
This is what you normally want to use, when a button is clicked, like "submit form"-actions.
Well I guess you could go for something in the area of MouseDown/MouseUp
<Button x:Name="Btn" MouseDown="MouseDown" MouseUp="MouseUp" />

Mouse events for captured element stop firing while dragging

I have some code that draws some points on the screen and then allows them to be dragged while holding the left mouse button. This kinda works except constantly the mouse events stop firing and the point being dragged stops moving.
Because all of the events stop being caught(for some unknown reason), that means the user can release the left mouse button without anything happening.
The weird thing is that the user can then reposition the mouse over the point and it will start dragging again without holding the left mouse button down. It makes for a very poor user experience.
What is going on?
pinPoint.MouseLeftButtonDown += Point_MouseLeftButtonDown;
pinPoint.MouseLeftButtonUp += Point_MouseLeftButtonUp;
pinPoint.MouseMove += Point_MouseMove;
.
.
.
void Point_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
((UIElement)sender).CaptureMouse();
if (_isDragging == false)
{
_isDragging = true;
}
}
void Point_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
_isDragging = false;
((UIElement)sender).ReleaseMouseCapture();
}
void Point_MouseMove(object sender, MouseEventArgs e)
{
//where the point gets moved and some other logic
//possibly this logic takes too long?
}
I've found a solution that works. I've still got no idea why it is continually losing the MouseCapture though.
pinPoint.LostMouseCapture += Point_LostMouseCapture;
.
.
.
void Point_LostMouseCapture(object sender, MouseEventArgs e)
{
//if we lost the capture but we are still dragging then just recapture it
if (_isDragging)
{
((UIElement)sender).CaptureMouse();
}
}

MouseWheel event issues on Controls

I need to get MouseWheel events on a PictureBox Control and according to another SO answer, getting MouseWheel stuff should be done like this:
void pic_MouseWheel(object sender, MouseEventArgs e)
{
if(e.Delta != 0)
{
MessageBox.Show(e.Delta.ToString());
}
}
But this does not work. Now messagebox is displayed and no breakpoints are being hit.
How do I capture the MouseWheel event on a PictureBox and also determine if the user scrolled the wheel UP or DOWN?
Nevermind, I figured it out. I need to Focus the PictureBox first:
void pic_Click(object sender, EventArgs e)
{
((PictureBox)sender).Focus();
}

Listbox events firing strangely

I'm confused. I am basically trying to tell when the user has clicked something in the listbox, held the button, and left the listbox. Here is a somewhat dumbed down version of what I am doing:
private bool itemHeld;
private void listOriginal_MouseDown(object sender, MouseEventArgs e)
{
itemHeld = true;
}
private void listOriginal_MouseUp(object sender, MouseEventArgs e)
{
itemHeld = false;
}
private void listOriginal_MouseLeave(object sender, EventArgs e)
{
if (itemHeld)
MessageBox.Show("OHH YEAH");
}
To me that seems like it should turn itemHeld true when you press the mousebutton, turn it false only if you lift it, and display ohh yeah if the value is true. If I break on the mouse down event to check the value, it is true and if I continue from there it displays the message. If I do not break, it does nothing. Is there something else at work here?
Edit:
Brief description: It would be difficult to explain what I am really trying to accomplish but imagine something almost like dragging a file off of a window. I need to simply be able to recognize when the user clicks inside of the listbox and then drags out of the listbox if that makes sense
You can not debug windows events by break point because when the Visual Studio get active to debug, the mouse leave event will be fired for the hovered control.
You can use Debug.WriteLine which writes information about the debug to the trace listeners.
private void button1_MouseLeave(object sender, EventArgs e)
{
Debug.WriteLine("Mouse leave");
}
private void button1_MouseEnter(object sender, EventArgs e)
{
Debug.WriteLine("Mouse enter");
}
private void button1_MouseHover(object sender, EventArgs e)
{
Debug.WriteLine("Mouse hover");
}
what about this?
private void listBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.X > listBox1.Width - 1 || e.Y > listBox1.Height - 1 || e.X < 0 || e.Y < 0)
{
Console.WriteLine("drag out");
}
else
Console.WriteLine("mouse move {0}/{1}", e.X, e.Y);
}
it uses the fact that the Control is not left before the mousebutton is released ... but be aware that the drag out part will occur more than once so you probably will want to have a flag set the first time ... and have that flag cleared on mouse up or leave
For every mouse click, your MouseDown event will fire AND your MouseUp event will fire, so the sequence of operations is equivalent to
itemHeld = true;
itemHeld = false;
if(itemHeld)
MessageBox.Show("yay");
If you press the mouse button on the listbox and move the cursor out without releasing the button, switching focus to another window (e.g. Visual Studio) is what triggers the MouseLeave event to fire. This is why you're seeing the message box pop up when you're debugging.
I'm not sure what you're trying to accomplish, so I can't recommend another solution.

How do I Manually Invoke/Raise Mouse Events in Silverlight 4?

In Silverlight 4, I wish to invoke one of the mouse button click events when the right mouse button is clicked. I have the RightMouseButtonDown click event wired up, but I don't know how to manually fire the MouseLeftButtonUp event.
I have tried raising the event in the following fashion.
private void MainLayoutRootMouseButton(object sender, MouseButtonEventArgs e)
{
MouseLeftButtonDown(sender, e);
}
However, a compiler error occurs:
"The event 'System.Windows.UIElement.MouseLeftButtonDown' can only appear on the left hand side of += or -=
What do I need to do to manually raise this event?
The other answers are correct in that they show you how to call those same code paths as you have for the other mouse events.
It should be clear that in Silverlight, you cannot raise (or automate) actual mouse button clicks, for security reasons.
Only user initiated actions such as the actual mouse moving can create real MouseEventArgs and fire the handlers directly, through the Silverlight input system.
You really shouldn't be raising the event itself. Instead, make the code inside the MouseLeftButtonUp its own function and then call that function from both the MouseLeftButtonUp and MouseRightButtonUp events.
e.g.
private void MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DoTheSameThing(sender, e);
}
private void MouseRightButtonDown(object sender, MouseButtonEventArgs e)
{
DoTheSameThing(sender, e);
}
private void DoTheSameThing(object sender, MouseButtonEventArgs e)
{
//Handle left or right mouse buttons up event
}
In your page's init function you need to have this line of code
someObjectOnPage.MouseLeftButtonDown += new MouseDownEventHandler(DoSomething);
someObjectOnPage.MouseRightButtonDown += new MouseDownEventHandler(DoSomething);
This is pure psuedo code but should lead you on the right track
also your method will need to look like this
void DoSomething(object sender, MouseButtonEventArgs e) { }

Categories