Lets go back to your original code listing :)
private bool leftButtonPressFlag = false;
private bool rightButtonPressFlag = false;
private void Form1_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) leftButtonPressFlag = true;
if (e.Button == MouseButtons.Right) rightButtonPressFlag = true;
if (leftButtonPressFlag && rightButtonPressFlag)
{
Color swap = ForeColor;
ForeColor = BackColor;
BackColor = swap;
}
}
private void Form1_MouseUp(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left) leftButtonPressFlag = false;
if (e.Button == MouseButtons.Right) rightButtonPressFlag = false;
}
The correct way will be as such:
if ((e.Button & MouseButtons.Left) != 0) leftButtonPressFlag = true;
if ((e.Button & MouseButtons.Right) != 0) rightButtonPressFlag = true;
if (leftButtonPressFlag && rightButtonPressFlag)
This is becos MouseButtons is a Flagged enum. Lets look at some real values now.
[Flags]
enum MouseButtons
{
Left = 0x 0010 0000,
....
Right = 0x 0020 0000,
....
}
Now the value of e.Buttons, if both buttons were pressed, would be 0x 0030 0000 (Left | Right), you should see now why your method will fail try to match either value, and hence will not work correctly. Note for single buttons your method will indeed work, but will fail matching combinations of buttons. I hope you understand :) If not, read abit about the binary (not boolean) operators of AND, OR, XOR, 1's complement. The windows calculator can actually help alot here! I remember it took me a good few months to understand it fully. :) top secret
Download xacc-ide 0.0.3 now!
See some screenshots