Jump to content

mouse events or any events in if statements

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
6 replies to this topic

#1
Siten0308

Siten0308

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 302 posts
Hello,

just like the title said, is there a way to put mouse events, keyboard events or any type of events into if statements? if so how, can you list a couple of examples?

Thank you

#2
scottk

scottk

    Learning Programmer

  • Members
  • PipPipPip
  • 35 posts
Do you mean assign an event based on an if statement?

    private void button4_Click(object sender, EventArgs e)

    {

      if (sender == button1)

        button1.Click += new EventHandler(button1_Click);

      else if (sender == button2)

        button2.Click += new EventHandler(button2_Click);

    }



#3
gaylo565

gaylo565

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 268 posts
Or did you mean call the button_Click Handle event manually without the button actually being pressed?

if(!buttonHasBeenPressedBeforeTimeOut)

button1_Click(null,null);
this probably isn't what you meant but with what scottk has up above as well you should be covered.

#4
relapse

relapse

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 476 posts
What are you trying to achieve?

#5
ArekBulski

ArekBulski

    Speaks fluent binary

  • Members
  • PipPipPipPipPipPipPipPip
  • 1,376 posts
This could also be adding delegates to events, with if-statements within. I do not think that was the question though.

private void Form1_Load(object sender, EventArgs e)

{

    button1.Click += new EventHandler(button1_Click);

}


void button1_Click(object sender, EventArgs e)

{

    throw new NotImplementedException();

}



#6
Eusébio Jaime

Eusébio Jaime

    Newbie

  • Members
  • Pip
  • 1 posts
If color of buttons background is white and i click on it with Button2 from the mouse then the counter2 should increase by one. All of the mouse listeners and events are working so is the Label and all the int's. It's just the statement is not increasing the counter when the action is performed.

#7
cdg10620

cdg10620

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 389 posts

Eusébio Jaime said:

If color of buttons background is white and i click on it with Button2 from the mouse then the counter2 should increase by one. All of the mouse listeners and events are working so is the Label and all the int's. It's just the statement is not increasing the counter when the action is performed.

If you are wanting a counter to increment when the button is clicked you need to increment it inside of the event. It would also be helpful if you posted your code. Your if statement should be something like this:

This block is more or less pseudo code. I'm not sure if it is complete but you should get the idea.
    private void button2_Click(object sender, EventArgs e)
    {
          if( button2.background == white )
          {
               counter2++;
          }
    }

-CDG10620
Software Developer