在用户控件上使用和重置MouseHover()事件 [英] Using and resetting the MouseHover() event on a user control

查看:59
本文介绍了在用户控件上使用和重置MouseHover()事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,

关于MouseHover事件,我有两个问题.

使用自定义控件,我在该拆分容器上绘制了图像,在拆分面板之一中,我绘制了RichTextBox控件.在运行时,我想让RichTextBox控件的OnMouseHover()事件触发时发生一些事情,但是我有两个 问题:

1.自定义控件上公开的MouseHover事件从不触发.

这显然是因为SplitContainer和RichTextBox控件将其遮盖了.我的问题是,如何公开RichTextBox的MouseHover()事件,以便将自定义控件绘制到窗体上的开发人员可以访问?尽管 同时取消UserControl的MouseHover事件.

2.我想重置MouseHover事件,因此调用ResetMouseEventArgs()可以有效地重置MouseHover状态.遵循此讨论的帖子(http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/6d016c17-ac21-49e9-977f-ffa5633578bd) 我的代码陷入了循环.如果我在RichTextBox控件上定义以下事件处理程序,即使鼠标不动,它也会持续触发.

Hello,

I have two problems around the MouseHover event.

Using a custom control I've painted onto that a split container and in one of the split panels I've paintined a RichTextBox control. At runtime I want to make something occur whenever the OnMouseHover() event of the RichTextBox control fires, but I have two problems:

1. The MouseHover event exposed on the custom control never fires.

This is obviously because it's being obscured by the SplitContainer and RichTextBox controls. My question is, how do I expose the RichTextBox's MouseHover() event so it's accessible to the developer who has painted the custom control onto their form? While at the same time suppressing the MouseHover event for the UserControl.

2. I want to reset the MouseHover event so call ResetMouseEventArgs() to effectively reset the MouseHover state. Following the postings from this discussion (http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/6d016c17-ac21-49e9-977f-ffa5633578bd) my code goes into a loop. If I define the following event handler on the RichTextBox control it continually fires, even if the mouse doesn't move.

protected override void OnMouseHover(EventArgs e)
{
  // Always call base implementations first. 
  base.OnMouseHover(e); 
  // Call ResetMouseEventArgs(), otherwise we won't get any more OnMouseHover() calls
  // until the user moves the mouse out of and back into this control. 
  this.ResetMouseEventArgs();
}

 

我怀疑解决方案是改为转到对RichTextBox的OnMouseMove()事件处理程序的ResetMouseEventArgs()调用,并完全取消OnMouseHover(),对吗?我实际上不想在自定义控件的MouseHover事件中做任何事情 水平.

谢谢
RTA.

 

I suspect the solution is to instead move to the ResetMouseEventArgs() call to the RichTextBox's OnMouseMove() event handler and do away with OnMouseHover() altogether, correct? I don't actually want to do anything in the MouseHover event at the custom control level.

Thank you,
RTA.

推荐答案

问题1:

您将需要进行反射以获取承载RichTextBox的父级MouseHover事件方法,然后在触发RichTextBox的MouseHover时调用该方法.叫做事件冒泡.

这是一篇演示该技术的文章: http://www.codeproject.com/KB/cs/event_broadcast.aspx

问题2:

Question 1:

You will need reflection to get the parent's MouseHover event method where your RichTextBox is hosted, then call that when RichTextBox's MouseHover has been triggered. It's called event bubbling.

Here's an article that demonstrate that technique: http://www.codeproject.com/KB/cs/event_broadcast.aspx

Question 2:

请尝试避免使用ResetMouseEventArgs方法,因为该方法仅供内部使用.而是改用MouseLeave事件.

Try to avoid using ResetMouseEventArgs method, as it is only for internal use. Instead, work around with MouseLeave event instead.


这篇关于在用户控件上使用和重置MouseHover()事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆