Click here to Skip to main content
16,006,065 members
Home / Discussions / C#
   

C#

 
AnswerRe: how do my program know that both buttons are down Pin
mav.northwind28-Aug-04 23:42
mav.northwind28-Aug-04 23:42 
GeneralRe: how do my program know that both buttons are down Pin
Umair Ahmad khan29-Aug-04 0:57
Umair Ahmad khan29-Aug-04 0:57 
GeneralRe: how do my program know that both buttons are down Pin
Vadim Gusev29-Aug-04 8:02
Vadim Gusev29-Aug-04 8:02 
GeneralRe: how do my program know that both buttons are down Pin
leppie29-Aug-04 8:57
leppie29-Aug-04 8:57 
GeneralRe: how do my program know that both buttons are down Pin
Vadim Gusev29-Aug-04 10:09
Vadim Gusev29-Aug-04 10:09 
GeneralRe: how do my program know that both buttons are down Pin
leppie29-Aug-04 10:24
leppie29-Aug-04 10:24 
GeneralRe: how do my program know that both buttons are down Pin
Vadim Gusev29-Aug-04 13:40
Vadim Gusev29-Aug-04 13:40 
GeneralRe: how do my program know that both buttons are down Pin
leppie29-Aug-04 21:26
leppie29-Aug-04 21:26 
Lets go back to your original code listing Smile | :)
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 Smile | :) 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.

Smile | :)

top secret
Download xacc-ide 0.0.3 now!
See some screenshots

Generalimagelist problem Pin
blankg28-Aug-04 22:10
blankg28-Aug-04 22:10 
Generalchallenging system.directoryservice Pin
vcorn28-Aug-04 18:50
vcorn28-Aug-04 18:50 
GeneralRe: challenging system.directoryservice Pin
vcorn28-Aug-04 20:37
vcorn28-Aug-04 20:37 
GeneralUTF7, UTF8, ASCII ..... I have problems Pin
Rostrox28-Aug-04 12:22
Rostrox28-Aug-04 12:22 
GeneralRe: UTF7, UTF8, ASCII ..... I have problems Pin
Steven Campbell28-Aug-04 12:44
Steven Campbell28-Aug-04 12:44 
GeneralRe: UTF7, UTF8, ASCII ..... I have problems Pin
Rostrox28-Aug-04 13:02
Rostrox28-Aug-04 13:02 
GeneralRe: UTF7, UTF8, ASCII ..... I have problems Pin
mav.northwind28-Aug-04 23:48
mav.northwind28-Aug-04 23:48 
GeneralRe: UTF7, UTF8, ASCII ..... I have problems Pin
eggie514-Sep-04 5:11
eggie514-Sep-04 5:11 
GeneralLoad image, modify and save Pin
Dominik Reichl28-Aug-04 11:20
Dominik Reichl28-Aug-04 11:20 
GeneralRe: Load image, modify and save Pin
Werdna30-Aug-04 6:45
Werdna30-Aug-04 6:45 
QuestionWay to reduce flicker on tab changes w/background image? Pin
jdrudolp28-Aug-04 8:11
jdrudolp28-Aug-04 8:11 
GeneralAssembly.LoadFrom and custom Type Editor issues Pin
viper299528-Aug-04 5:46
viper299528-Aug-04 5:46 
GeneralRe: Assembly.LoadFrom and custom Type Editor issues Pin
Marc Clifton28-Aug-04 16:12
mvaMarc Clifton28-Aug-04 16:12 
GeneralMemory Error Pin
dbetting28-Aug-04 4:32
dbetting28-Aug-04 4:32 
GeneralRe: Memory Error Pin
dbetting28-Aug-04 10:17
dbetting28-Aug-04 10:17 
GeneralTo change an int or float to text Pin
TehGT28-Aug-04 4:09
TehGT28-Aug-04 4:09 
GeneralRe: To change an int or float to text Pin
Mazdak28-Aug-04 4:44
Mazdak28-Aug-04 4:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.