Muhammad Ali Dildar
Muhammad Ali Dildar

Reputation: 1527

How to check whether control is button or not?

How can I check that whether mouse pointer is pointing a button or some other control? I want to perform a particular task when mouse hover/move a button.

I know I can set event on individual button. But isn't it possible to check the pointed/hover control is button?

Upvotes: 2

Views: 9563

Answers (2)

Gert Arnold
Gert Arnold

Reputation: 109080

I'm not sure if you mean: can I do this without event handlers for MouseHover in individual controls. If so, the answer is no.

But you can attach each contol's MouseHover event to just one event handler that could look like the one in Chris's answer. For convenience you could even do that programmatically by looping through the controls in the form's load event. (assuming this is winforms)

Upvotes: 2

Chris Marasti-Georg
Chris Marasti-Georg

Reputation: 34650

The sender argument in an event method should have the information you need...

private void MyEventHandler(object sender, EventArgs args) {
   if(sender is Button) {
      //Do some stuff
   }
}

Upvotes: 12

Related Questions