Intro
I have a WPF application.
When i click/double click on a button to show next screen, this is captured with both MouseLeftButtonDown and MouseLeftButtonUp.
- MouseLeftButtonDown is making the button darker
- MouseLeftButtonUp is sending me to next screen
The problem:
If i "spam" or sometimes just click 2-3 (we say 3 times in this case) times on the button, it's start loading the next screen. When the next screen is shown, two mouse clicks is left in the queue and if the mouse is over another new button at the second screen, this is clicked on.
I tried with stuff like:
void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Mouse.OverrideCursor = Cursors.Wait;
if (this.IsVisible && this.IsLoaded)
OnButtonPatient();
}
but both properties is set to true.
I guess thats right since i can see the button when the mouse event triggers.
The events can be explained like:
3 mouse clicks
mouse cursor = waiting cursor
next screen is loaded
mouse cursor = normal
mouse cursor = waiting cursor
next button is clicked
How can i handle this?
I dont want mouse event that happend on a previously screen follow on to my next screen.
Greetings!
I tried a simple example using StackPanel as control. Although I am not sure why you are not using Button Click event if you are using a button (Why move on ButtonUp?)
private int count = 0;
private void StackPanel_OnMouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
if (count == 0 && (sender as StackPanel).IsMouseOver)
{
Mouse.OverrideCursor = Cursors.Wait;
count++;
}
e.Handled = true;
}
private void StackPanel_OnMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
if (count == 1 && (sender as StackPanel).IsMouseOver)
{
Mouse.OverrideCursor = Cursors.Arrow;
count++;
}
e.Handled = true;
}
I would suggest checking the IsMouseOver property on your first window/screen when the MouseUp event fires. This would let you filter out events where the new window is blocking the first.
void LayoutRoot_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
Mouse.OverrideCursor = Cursors.Wait;
if (this.IsVisible && this.IsLoaded && this.IsMouseOver)
OnButtonPatient();
}
Related
I have an image that will instantly show a popup when mouse hovers over it but I want it to wait for a bit before it shows the popup but also have it so that when mouse moves away from the image in will not appear if it didn't already.
I think that the best way to do it is to use a timer that starts when Mouse enters the picture and gets discarded when the mouse leaves the picture.
private async void img_MouseEnter(object sender, MouseEventArgs e)
{
Timer.Interval = TimeSpan.FromSeconds(2)
//when timer finished
popup.IsOpen = true;
}
private void img_MouseLeave(object sender, MouseEventArgs e)
{
Timer.Stop();
popup.IsOpen = false;
}
i need to implement hover areas to my Tool.
When i enter the last ~ 25% of the Window my Item should appear.
I tried to make an invisible Grid but an invisible object cant trigger events.
Is it possible to make a hover area with the Mouse Position?
private void BlackMetalClockRing_MouseLeave(object sender, MouseEventArgs e)
{
gridExpandInfo.Visibility = Visibility.Hidden;
}
private void BlackMetalClockRing_MouseEnter(object sender, MouseEventArgs e)
{
if (gridInformationPanel.Visibility != Visibility.Visible)
gridExpandInfo.Visibility = Visibility.Visible;
}
Associate the MouseEnter event to a new Border that'll cover your grid, with Background="Transparent". Set yourBorder.Visibility = Visibility.Collapsed at the end of this event (in order to make sure it wont intercept further mouse events).
In the MouseLeave event (which stays associated to your grid), set back yourBorder.Visibility = Visibility.Visible.
I have a ListView with the View set to LargeIcon.
I specifically need to detect when multiple items were selected by dragging a selection box around them with the mouse.
(For example I don't want to know when items were selected by CTRL + Click)
I thought I could simply do it by keeping track of whether the mouse was down while it was moving which would indicate dragging, then on mouse up if it was a drag then I can set another variable to indicate this.
In my example below mouseDown is set to true, but when I keep the mouse down and move it isDrag is never set to true and I can't see what I'm doing wrong.
(Edit: isDrag becomes true if I remove the if clause which is weird because as I said mouseDown is definitely true).
I realise the code is a little longer than it needs to be but it's for clarity.
bool mouseDown;
bool isDrag;
bool wasDrag;
private void listView1_MouseDown(object sender, MouseEventArgs args)
{
wasDrag = false;
mouseDown = true;
}
private void listView1_MouseMove(object sender, MouseEventArgs args)
{
if (mouseDown)
isDrag = true; // <-- Never becomes true, even though mouseDown is true
}
private void listView1_MouseUp(object sender, MouseEventArgs args)
{
if (isDrag)
wasDrag = true;
mouseDown = false;
isDrag = false;
}
I know it'll be something stupid. Please put me out of my misery.
Alternatively if someone knows of a better was to detect a dragging selection (what's the proper term?) then I'm all ears.
Can you try this:
private void listView1_MouseMove(object sender, MouseEventArgs args)
{
isDrag = mouseDown;
}
I think for some reason, your event listView1_MouseUp still fires, which makes your isDrag variable set to other than the intended value. Try to put breakpoints on both MouseMove and MouseUp events to see the sequence with which they are firing.
After further investigation I've discovered that for a ListView control the MouseMove event will not fire while MouseDown is still occurring and fires immediately after releasing the mouse.
I can only assume that the logic built into this control that allows you to select multiple files by dragging a selection is messing with these events and essentially making them synchronous.
I've put together a basic workaround for this. It's not ideal but it does the job so I thought I'd share.
Basically when the mouse goes down I record the position. When the mouse goes up I check to see if it has moved more than a certain distance in any direction. If it has not I consider it a click, if it has I consider it a drag.
// Records the mouse position on mousedown
int beforeMoveX;
int beforeMoveY;
// How far in pixels the mouse must move in any direction
// before we consider this a drag rather than a click
int moveBounds = 20;
private void listView1_MouseDown(object sender, MouseEventArgs e)
{
// Save the mouse position
beforeMoveX = e.X;
beforeMoveY= e.Y;
}
private void listView1_MouseUp(object sender, MouseEventArgs e)
{
// Did we move more than the bounds in any direction?
if (e.X < (beforeMoveX - moveBounds) ||
e.X > (beforeMoveX + moveBounds) ||
e.Y < (beforeMoveY - moveBounds) ||
e.Y > (beforeMoveY + moveBounds))
{
// DRAGGED!
}
else
{
// NOT DRAGGED!
}
}
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;
}
I want to create a card playing game. I the use mousemove event to drag cards through the window. The problem is if I move the mouse over another card, it is stuck because the card underneath the mouse cursor gets the mouse events, so that the MouseMove event of the window isn't fired.
This is what I do:
private void RommeeGUI_MouseMove(object sender, MouseEventArgs e)
{
if (handkarte != null)
{
handkarte.Location = this.PointToClient(Cursor.Position);
}
}
I tried the following, but there was no difference:
SetStyle(ControlStyles.UserMouse,true);
SetStyle(ControlStyles.EnableNotifyMessage, true);
Iam looking for a way to implement an application-global event-handler or a way to implement so called event-bubbling. At least I want to make the mouse ignore certain controls.
In order to do this you will need to keep track of a few things in your code:
On which card the mouse is pointing
when the mouse button is pressed;
this is the card that you want to
move (use the MouseDown event)
Move the the card when the mouse is moved
Stop moving the card when the mouse button is released (use the
MouseUp event)
In order to just move around controls, there is no need to actually capture the mouse.
A quick example (using Panel controls as "cards"):
Panel _currentlyMovingCard = null;
Point _moveOrigin = Point.Empty;
private void Card_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
_currentlyMovingCard = (Panel)sender;
_moveOrigin = e.Location;
}
}
private void Card_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && _currentlyMovingCard != null)
{
// move the _currentlyMovingCard control
_currentlyMovingCard.Location = new Point(
_currentlyMovingCard.Left - _moveOrigin.X + e.X,
_currentlyMovingCard.Top - _moveOrigin.Y + e.Y);
}
}
private void Card_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left && _currentlyMovingCard != null)
{
_currentlyMovingCard = null;
}
}
What you could do, is send the MouseDown event to the event.function you want to call.
Say you got a "Label" ontop of a "Card", but you wan't to "go-through" it:
private void Label_MouseDown( object sender, MouseEventArgs)
{
// Send this event down the line!
Card_MouseDown(sender, e); // Call the card's MouseDown event function
}
Now the appropriate event-function is called, even though the bothersome label was clicked.
Normally you do this by, before capturing the mouse, seeing if anybody else has it...