How can I detect, which mouse button have double clicked the form i.e. Left, Right or Middle?
Updated:
I am using .NET2.0
Store the last clicked button in MouseUp event and then check that in the double click event. Sample code:
MouseButtons _lastButtonUp = MouseButtons.None;
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
_lastButtonUp = e.Button;
}
private void Form1_DoubleClick(object sender, EventArgs e)
{
switch (_lastButtonUp)
{
case System.Windows.Forms.MouseButtons.Left:
MessageBox.Show("left double click");
break;
case System.Windows.Forms.MouseButtons.Right:
MessageBox.Show("right double click");
break;
case System.Windows.Forms.MouseButtons.Middle:
MessageBox.Show("middle double click");
break;
}
}
Have a look at MouseDoubleClick and MouseEventArgs and MouseButtons Enumeration
MouseDoubleClick is one of the Form events.
In Whatever_Click or DoubleClick event, you can check the MouseEventArgs e, which contains what key was pressed.
private void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
Console.WriteLine("Left Mouse Button was clicked!");
else if (e.Button == MouseButtons.Middle)
Console.WriteLine("Middle Mouse Button was clicked!");
}
Other buttons include MouseButtons.Right, MouseButtons.Left
in form_MouseDoubleClick event you can trace
void Form1_MouseDoubleClick(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
// Do Operation
}
}
Related
I everyone,
I have development this application, ... my objective is user, if click on right side of the mouse the url is paste to textbox.
MenuStrip
But when i click in right side, appear the menustrip of textbox. My question is I can disable this "menustrip"?!?
I send my code at the moment:
private void TxtUrl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == System.Windows.Forms.MouseButtons.Right)
TxtUrl.Paste();
}
This should work for your code:
private MouseButtons e_Button = new MouseButtons();
private void TxtUrl_MouseDown(object sender, MouseEventArgs e)
{
e_Button = e.Button;
if (e.Button == System.Windows.Forms.MouseButtons.Right)
TxtUrl.Paste();
}
private void cms_Opening(object sender, CancelEventArgs e)
{
if (e_Button == System.Windows.Forms.MouseButtons.Right)
e.Cancel = true;
}
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"/>
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();
}
}
I want to be able to use the space key to modify the behavior of the mouse while it is held down. Without knowing better I am imagining it involves some kind of coordination between two (or three) event handler - mousemove, keydown, and keyup. But I am wondering if there is some way to handle it entirely within one event handler - mousemove.
Example code to give an idea of what I hope to be able to do...
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (Keyboard.KeyDown == Keys.Space)
{
/* Do modified behavour for left mouse being held down while
space is also held down */
}
else
{
// Do normal behavour for left mouse being held down
}
}
}
Is something like this possible or will I have to save the state of the space key to a class variable using keydown event handler and check it with the mouse move handler?
IT could be done using Control.ModifierKeys & Control.MouseButtons. But only works for kays like shift, ctrl and alt.
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
if ((Control.ModifierKeys & Keys.Shift) != 0)
{
if ((Control.MouseButtons & MouseButtons.Left) != 0)
{ // here you go
}
}
}
You should set a variable in your KeyDown-Event and check it in your MouseEvent:
bool buttonpressed = false;
private void KeyDown_Event(object s, System.Windows.Forms.KeyEventArgs e)
{
if(e.KeyCode == KeyCode.Space)
buttonpressed = true;
else
buttonpressed = false;
}
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (buttonPressed)
{
/* Do modified behavour for left mouse being held down while
space is also held down */
}
else
{
// Do normal behavour for left mouse being held down
}
}
}
I'd have a variable that tracks when Keys.Space is in the Pressed state, then trigger it with KeyUp and KeyDown events
private void pictureBox1_MouseMove(object sender, MouseEventArgs e)
// Check if Key.Space pressed
if(SpacePressed) {
// Do something
}
}
private void KeyPressed_Event(object sender, KeyEventArgs e) {
// Check if Key.Space pressed
if(e.Key == Key.Space) {
SpacePressed = true;
}
}
private void KeyRelease_Event(object sender, KeyEventArgs e) {
// Check if Key.Space pressed
if(e.Key == Key.Space) {
SpacePressed = false;
}
}
You could use PInvoke to call GetKeyState in User32.dll.
How can I call a method when I press a button and call another when I release the button?
I'm working with a PTT (press to talk) button in my C# WinForms app.
Use MouseDown and MouseUp events
private void button1_MouseDown(object sender, MouseEventArgs e)
{
// button is being pressed
}
private void button1_MouseUp(object sender, MouseEventArgs e)
{
// button was released
}
Maybe something like this perhaps:
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
Console.WriteLine("Mouse Button was pressed down on the button");
}
private void btn1_MouseUp(object sender, MouseEventArgs e)
{
Console.WriteLine("Mouse button Button was released");
}
Take a look at the output windows after clicking the button.
Updated
Ok try this, add a text box to your form and name it TextBox1 and then add this code to the code behind:
private void btn1_MouseDown(object sender, MouseEventArgs e)
{
this.TextBox1.Text = "Mouse Button was pressed down on the button";
}
private void btn1_MouseUp(object sender, MouseEventArgs e)
{
this.TextBox1.Text = "Mouse button Button was released";
}