Enum constant for "Left + Right" click of MouseButton.

C

Chaitanya

Hello, In my Application i want to know when user clicks both the
"Left" and "Right" buttons of the Mouse.
I am getting a number like this "3145728". But the MouseButtons Enum
contains only Left, Right, Middle, None, XButton1 and XButton2.
I am using .NET 1.1 version.

How can i convert "3145728" to MouseButtons Enum? why Mouse Left+Right
click not added to the Enum? Can anyone help me?
 
D

Daniel

For combined you use the &&, for example

if(Mouse.Right && Mouse.LEFT)
//do something
 
C

Chaitanya

DeveloperX said:
Where are you getting that number?

Press Left and Right buttons of the mouse and then Drag.
On MouseMove event, i checked e.Button.
Its showing e.Button = 3145728
 
D

Daniel

To recover from my earlier error:

This is how you could do it and this does work, check your output tab as you
click:

public partial class Form1 : Form

{

public bool _mouseLeft = false;

public bool _mouseRight = false;

public Form1()

{

InitializeComponent();

}

private void Form1_MouseDown(object sender, MouseEventArgs e)

{

MouseButtons m = e.Button;

switch (m)

{

case MouseButtons.Left:

_mouseLeft = true;

break;

case MouseButtons.Right:

_mouseRight = true;

break;

}

if (_mouseRight && _mouseLeft)

Console.WriteLine("Both buttons are pressed");

Console.WriteLine("Button down " + m.ToString());

}

private void Form1_MouseUp(object sender, MouseEventArgs e)

{

MouseButtons m = e.Button;

switch (m)

{

case MouseButtons.Left:

_mouseLeft = false;

break;

case MouseButtons.Right:

_mouseRight = false;

break;

}

Console.WriteLine("Button up " + m.ToString());

}

}
 
D

Daniel

I hope this doesnt post twice, i had a prob first time but this how you
could do it, check your output tab for results as you click:

public partial class Form1 : Form

{

public bool _mouseLeft = false;

public bool _mouseRight = false;

public Form1()

{

InitializeComponent();

}

private void Form1_MouseDown(object sender, MouseEventArgs e)

{

MouseButtons m = e.Button;

switch (m)

{

case MouseButtons.Left:

_mouseLeft = true;

break;

case MouseButtons.Right:

_mouseRight = true;

break;

}

if (_mouseRight && _mouseLeft)

Console.WriteLine("Both buttons are pressed");

Console.WriteLine("Button down " + m.ToString());

}

private void Form1_MouseUp(object sender, MouseEventArgs e)

{

MouseButtons m = e.Button;

switch (m)

{

case MouseButtons.Left:

_mouseLeft = false;

break;

case MouseButtons.Right:

_mouseRight = false;

break;

}

Console.WriteLine("Button up " + m.ToString());

}

}
 
D

DeveloperX

Oh I see, sorry, this works, it will print . unless the left button is
held down regardless of what other buttons are held down at the same
time, the buttons are or'ed together.
if(((int)e.Button & (int)MouseButtons.Left) > 0)
{
Console.WriteLine("Left");
}
else
{
Console.WriteLine(".");
}



Console.WriteLine(((int)e.Button).ToString()); shows that all the
individual buttons are powers of 2.
 
H

Hans Kesting

Hello, In my Application i want to know when user clicks both the
"Left" and "Right" buttons of the Mouse.
I am getting a number like this "3145728". But the MouseButtons Enum
contains only Left, Right, Middle, None, XButton1 and XButton2.
I am using .NET 1.1 version.

How can i convert "3145728" to MouseButtons Enum? why Mouse Left+Right
click not added to the Enum? Can anyone help me?

MouseButtons.Right = 2097152, MouseButtons.Left = 1048576. When you add
them, you get your number.

You can combine values yourself:
MouseButtons.Right | MouseButtons.Left

That's what the "This enumeration has a FlagsAttribute attribute that
allows a bitwise combination of its member values" remark is about in
http://msdn2.microsoft.com/en-us/library/system.windows.forms.mousebuttons(VS.71).aspx


Hans Kesting
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top