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;
}
Related
I'm doing some kind of a pixel film editor and my problem there is that I am using Labels which I have to click individually to change their color, I would like to change the color by holding down the mouse button and just hovering above the Label.
Is there any Event or such to do so? I didn't find one. At the moment I am using Mouse_Down.
A look on the Editor
my Mouse_Down Event:
private void Mouse_Down(object sender, MouseEventArgs e)
{
var ActiveLabel = sender as Label;
if (ActiveLabel != null)
{
ActiveLabel.BackColor = ActiveColor.BackColor;
}
}
So I want it like on GIMP or stuff, to not click any single "pixel" but instead click and hold the mouse button and move it around and color all pixels I move over whilst my mouse is pressed.
Here's the idea:
You have a flag (boolean) which indicates whether the user is currently painting or not. If the mouse hovers over a label (event MouseEnter), you check whether it should be painted or not. If it shall be painted, you apply the code you already have.
bool paintmode = false;
// Assign this to all labels, since you don't know
// where the user will start painting
private void Mouse_Down(object sender, MouseEventArgs e)
{
paintmode = true;
}
// Make sure you assign this to all items, even the Form
// as you'll never know where the user will release the mouse
private void Mouse_Up(object sender, MouseEventArgs e)
{
paintmode = false;
}
// Assign this only to labels which can get painted
private void Mouse_Enter(object sender, MouseEventArgs e)
{
if (paintmode)
{
var ActiveLabel = sender as Label;
if (ActiveLabel != null)
{
ActiveLabel.BackColor = ActiveColor.BackColor;
}
}
}
This question already has answers here:
Why doesn't doubleclick event fire after mouseDown event on same element fires?
(2 answers)
Closed 5 years ago.
I have a MenuStrip from which I want to drag some things on the form body(then some things happen such as the backcolor of the form is changing, etc). I am handling the MouseDown event, but the thing is that when I click on the option in the ToolStripMenu the same things happen(the backcolor of the form is changing, etc).
What I want is to somehow separate the MouseClick from the MouseDown. More precisely, when I click on one option of the MenuStrip I don't want anything to happen.
When I click, the MouseDown event fires. I want to ignore it unless the mouse's cursor moves.
private void salmonToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
{
//gets the cursor position at the moment when mouse down is activated
p1M = Cursor.Position.X;
p2M = Cursor.Position.Y;
//miscare shows if the mouse moved
if (miscare == true)
{
//do things
}
}
private void Rezervare1_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void salmonToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
{
//gets the cursor position to see if it moved
p1m = Cursor.Position.X;
p2m = Cursor.Position.Y;
if (p1M != p1m || p2M != p2m)
{
miscare = true;//it means the cursor moved
}
else miscare = false;
}
If you want your logic to happen on mouse move, then lets handle MouseMove!
bool isMouseDown = false;
private void salmonToolStripMenuItem_MouseDown(object sender, MouseEventArgs e)
{
isMouseDown = true;
}
private void salmonToolStripMenuItem_MouseUp(object sender, MouseEventArgs e)
{
isMouseDown = false;
}
private void salmonToolStripMenuItem_MouseMove(object sender, MouseEventArgs e)
{
if(isMouseDown)
{
//Do your thing
}
}
Also note, there are some strange edge cases you can hit with things like the form losing focus, or the user dragging outside of the bounds of the form. Something to be aware of and handle as needed.
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;
}
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();
}
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;
}