简体   繁体   English

如何检查控件是否为按钮?

[英]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? 但是,不能检查指向/悬停控件是否为按钮吗?

The sender argument in an event method should have the information you need... 事件方法中的sender参数应具有所需的信息...

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

I'm not sure if you mean: can I do this without event handlers for MouseHover in individual controls. 我不确定您的意思是:我可以在没有单个控件中的MouseHover事件处理程序的情况下执行此操作吗? 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. 但是您可以将每个contol的MouseHover事件附加到一个事件处理程序,该事件处理程序看起来像Chris的答案中的事件处理程序。 For convenience you could even do that programmatically by looping through the controls in the form's load event. 为方便起见,您甚至可以通过循环访问表单的load事件中的控件来以编程方式进行操作。 (assuming this is winforms) (假设这是winforms)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM