Trigger mouseup event on keyup - WPF - c#

My question is simple. Is it possible to trigger mouseup and mousedown on keyup and keydown event respectivey in WPF.
I have set of event handlers for mouse down and mouse up in my custom control. I want to trigger the same if the user try to press the spacebar on my custom button.
private void CustomButton_MouseDown(object sender, MouseButtonEventArgs e)
{
eventlabel.Content = "Mousedown event Triggered";
}
private void CustomButton_MouseUp(object sender, MouseButtonEventArgs e)
{
eventlabel.Content = "MouseUp event Triggered";
}
private void CustomButton_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
//Need to trigger all the handlers of mousedown
}
}
private void CustomButton_KeyUp(object sender, KeyEventArgs e)
{
if (e.Key == Key.Space)
{
//Need to trigger all the handlers of mouseup
}
}

You can raise Events in all UIElements with RaiseEvent(EventArgs). The MouseButtonArgsEventsArgs can be used as followed in the KeyDown-EventHandler:
MouseDevice mouseDevice = Mouse.PrimaryDevice;
MouseButtonEventArgs mouseButtonEventArgs = new MouseButtonEventArgs(mouseDevice, 0, MouseButton.Left);
mouseButtonEventArgs.RoutedEvent = Mouse.MouseDownEvent;
mouseButtonEventArgs.Source = this;
RaiseEvent(mouseButtonEventArgs);
(modified example from https://social.msdn.microsoft.com/Forums/vstudio/de-DE/7ec6b75b-b808-48ca-a880-fafa9025da6e/how-to-raise-a-click-event-on-an-uielement?forum=wpf)

Answer mentioned by Tobias works for me. But, it need some tweak.
elementName.RaiseEvent(new MouseButtonEventArgs(Mouse.PrimaryDevice, 0, MouseButton.Left)
{
RoutedEvent = Mouse.MouseDownEvent,
Source = this,
});
Here is the Reference

Related

Button.MouseMove event in WPF always has e.LeftMouse with status Released

I am trying to start a drag and drop on a Button control in WPF. I am using Button because I also want to handle click event.
Adding a Button in XAML and handling the MouseMove event always has e.LeftMouse equal to MouseButtonState.Released.
<Button MouseMove="Button_MouseMove"/>
In the following handler implementation the exception is never thrown.
private void Button_MouseMove(object sender, MouseEventArgs e)
{
if (e.LeftButton == MouseButtonState.Pressed)
throw new Exception("It works!");
}
I noticed the same is true for any control I place inside a Button and try to process MouseMove event.
How do I handle drag and drop from a Button control or any other control inside a button in WPF?
EDIT - Solution based on mm8's answer
The updated XAML is:
<Button PreviewMouseDown="Button_PreviewMouseDown"
PreviewMouseUp="Button_PreviewMouseUp"
PreviewMouseMove="Button_PreviewMouseMove">
</Button>
The updated handler code:
Point startPosition;
double delta = 10;
private void Button_PreviewMouseDown(object sender, MouseButtonEventArgs e)
{
e.Handled = true;
startPosition = e.GetPosition(this);
}
private void Button_PreviewMouseUp(object sender, MouseButtonEventArgs e)
{
Point currentPosition = e.GetPosition(this);
if ((currentPosition - startPosition).Length < delta)
throw new Exception("MouseClick");
}
private void Button_PreviewMouseMove(object sender, MouseEventArgs e)
{
Point currentPosition = e.GetPosition(this);
double currentDelta = (currentPosition - startPosition).Length;
if (e.LeftButton == MouseButtonState.Pressed && currentDelta >= delta)
throw new Exception("DragAndDrop");
}
You could perhaps handle the PreviewMouseDown event instead of Click:
private void Button_Click(object sender, MouseButtonEventArgs e)
{
//handle click here...
e.Handled = true;
}
XAML:
<Button PreviewMouseDown="Button_Click" MouseMove="Button_MouseMove"/>

MouseDown / MouseLeftButtonDown events not firing, MouseUp is?

Here's my code:
public AbilitiesController(Abilities page)
{
_page = page;
page.ScrollBar.MouseLeftButtonDown += MouseDown;
page.ScrollBar.MouseLeftButtonUp += MouseUp;
page.ScrollBar.MouseLeave += MouseLeave;
page.ScrollBar.MouseWheel += MouseWheel;
page.ScrollBar.MouseMove += MouseMove;
}
private void MouseMove(object sender, MouseEventArgs mouseEventArgs)
{
if (!_dragBound) return;
var newPos = mouseEventArgs.GetPosition(_page);
var dPos = newPos - _pos;
_page.ScrollBar.ScrollToHorizontalOffset(dPos.X);
_page.ScrollBar.ScrollToVerticalOffset(dPos.Y);
_pos = newPos;
Console.WriteLine("Moved");
}
private void MouseWheel(object sender, MouseWheelEventArgs mouseWheelEventArgs)
{
Console.WriteLine("MouseWheel");
}
private void MouseLeave(object sender, MouseEventArgs mouseEventArgs)
{
_dragBound = false;
Console.WriteLine("Left");
}
private void MouseUp(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
_dragBound = false;
Console.WriteLine("Mouse Up");
}
private void MouseDown(object sender, MouseButtonEventArgs mouseButtonEventArgs)
{
_dragBound = true;
Console.WriteLine("Click!");
}
Page is a Page, Scrollbar is a Scrollview.
I know my logic for moving the scrollbar probably isn't correct yet, but we aren't even getting that far yet.
For some odd reason the MouseDown event isn't firing whether I bind it to MouseDown or MouseLeftButtonDown.
However oddly enough the MouseUp event works no problem.
Which seems really odd because normally if one is broken, the other is too...
Using latest version of Visual Studio 2017 in a WPF project.
You might want to try 'PreviewMouseDown' instead of 'MouseDown' and same for all the other non-working mouse events, as they may already be being handled by whatever the base class of your 'Abilities' class is.

How to Delete a user control through key down event

I have a User control which is a label along with a text box. this control will created dynamically in a form. i want to delete this control using delete key press.the Click event is used to focus on the control and keypress method used to trigger the keydown event. Here is my code
private void usereditFieldControl_Click(object sender, EventArgs e)
{
EditFieldControl editFieldControl = (EditFieldControl)sender;
editFieldControl.KeyDown += new KeyEventHandler(Key_pressed);
}
private void Key_pressed(object sender, KeyEventArgs e)
{
EditFieldControl editFieldControl = (EditFieldControl)sender;
if (e.KeyCode == Keys.Delete)
{
editFieldControl.Dispose();
}
}
You can use the following code. "sender" can be cast to the more generic "Control" class:
private void textBox1_KeyDown(object sender, KeyEventArgs e) {
if (e.KeyCode == Keys.Delete) {
Control ctl = (Control)sender;
ctl.Dispose();
this.Controls.Remove(ctl);
}
}
If your control is created dinamically at runtime, do not forget to add a handler to KeyDown event, with something like this:
this.textBox1.KeyDown += new System.Windows.Forms.KeyEventHandler(this.textBox1_KeyDown);
private void usereditFieldControl_Click(object sender, EventArgs e)
{
EditFieldControl editFieldControl = (EditFieldControl)sender;
editFieldControl.KeyDown += new KeyEventHandler(Key_pressed);
}
private void Key_pressed(object sender, KeyEventArgs e)
{
EditFieldControl editFieldControl = (EditFieldControl)sender;
if (e.KeyCode == Keys.Delete)
{
//Find control
for (int i = 0; i < editFieldControl.Parent.Controls.Count(); i++) {
if (editFieldControl.Parent.Controls[i].Name == editFieldControl.Name) {
//Unhook events to prevent memory leaks
editFieldControl.KeyDown -= new KeyEventHandler(Key_pressed);
//Remove control from collection
editFieldControl.Parent.Controls.RemoveAt[i];
break;
}
}
//repaint
this.Invalidate();
}
}
Untested, written on iPhone

Drag a WPF window around the desktop with a button

I have this code for dragging my Window with its MouseDown Event.
private void Window_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == MouseButton.Left)
this.DragMove();
}
But I want to do this using a Button, because my form is transparent. And Using the same function for that button's MouseDown event will not work.
How can I achieve this?
You have to use PreviewMouseDown instead of MouseDown event.
And do not forget to mark the event "Handled" in the end.
XAML code:
<Button x:Name="Button_Move" PreviewMouseDown="Window_Main_MouseDown"/>
C# code:
private void Window_Main_MouseDown(object sender, MouseButtonEventArgs e)
{
if (e.ChangedButton == System.Windows.Input.MouseButton.Left)
{
this.DragMove();
e.Handled = true;
}
}
Use border instead of Button. Because DragMove only work on PrimaryMouseButton event. Not work on Click event
XAML
<Border Background="Blue" MouseLeftButtonDown="Border_MouseLeftButtonDown">
</Border>
CODE
private void Border_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
DragMove();
}
My Finally Code was here:
private Point startPoint;
private void btnChat_PreviewMouseLeftButtonUp(object sender, MouseButtonEventArgs e)
{
startPoint = e.GetPosition(btnChat);
}
private void btnChat_PreviewMouseMove(object sender, MouseEventArgs e)
{
var currentPoint = e.GetPosition(btnChat);
if (e.LeftButton == MouseButtonState.Pressed &&
btnChat.IsMouseCaptured &&
(Math.Abs(currentPoint.X - startPoint.X) >
SystemParameters.MinimumHorizontalDragDistance ||
Math.Abs(currentPoint.Y - startPoint.Y) >
SystemParameters.MinimumVerticalDragDistance))
{
// Prevent Click from firing
btnChat.ReleaseMouseCapture();
DragMove();
}
}

C# DragDrop from toolStrip

I'm looking for a way to determine which item in a toolStrip that was dragged after a DragDrop event has occured, all I want to do is make a switch case with different cases for each item in the toolstrip but I cant seem to find a way of comparing them.
UPDATE: SHORT CODESAMPLE
private void toolStrip1_DragDrop(object sender, DragEventArgs e)
{
//Here I want something like a DoDragDrop() and send the specific item from the
//toolstrip..
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
//And here some way to determine which of the items was dragged
//(I'm not completely sure if I need a mouseUp event though..)
}
Hopefully a bit easier to get what I'm trying to do.
The events in your example don't look like the correct events to use.
Here is a working example from a ToolStrip that has 2 ToolStripButtons on it:
public Form1() {
InitializeComponent();
toolStripButton1.MouseDown += toolStripButton_MouseDown;
toolStripButton2.MouseDown += toolStripButton_MouseDown;
panel1.DragEnter += panel1_DragEnter;
panel1.DragDrop += panel1_DragDrop;
}
void toolStripButton_MouseDown(object sender, MouseEventArgs e) {
this.DoDragDrop(sender, DragDropEffects.Copy);
}
void panel1_DragEnter(object sender, DragEventArgs e) {
e.Effect = DragDropEffects.Copy;
}
void panel1_DragDrop(object sender, DragEventArgs e) {
ToolStripButton button = e.Data.GetData(typeof(ToolStripButton))
as ToolStripButton;
if (button != null) {
if (button.Equals(toolStripButton1)) {
MessageBox.Show("Dragged and dropped Button 1");
} else if (button.Equals(toolStripButton2)) {
MessageBox.Show("Dragged and dropped Button 2");
}
}
}

Categories