C#的WinForms用户控件鼠标事件帮助 [英] C# WinForms UserControl Mouse Event Help

查看:184
本文介绍了C#的WinForms用户控件鼠标事件帮助的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我为我的项目创建一个自定义的控制。在这个控制有像一个标签,一个图片,和几个LinkLabel的子控件。其他然后LinkLabel的,我要的鼠标事件目前在父母的控制,并具有对鼠标的控制响应。当你在控制徘徊,但背景颜色不超过时,子控件改变背景颜色的变化;这是因为没有对孩子没有控制的MouseEnter和鼠标离开事件。我通过添加父控件委托方法的子控件解决了这个问题。问题仍然是,当我订阅了click事件对我的父控件的Click事件也被忽略了的子控件。我可以订阅每个儿童的控制,但我如何强制父控件的Click事件?我通过搜索发现这个词是事件冒泡,但这似乎只适用于ASP.NET技术和框架。有什么建议?

I have a custom control that I created for my project. In this control there are several child controls like a Label, a PictureBox, and a LinkLabel. Other then the LinkLabel, I want the mouse over event currently on the parent control and have the control respond to the mouse over. The background color changes when you hover over the control, but the background color doesn't change when over a child control; this is because there is no MouseEnter and MouseLeave events on the child control. I solved this issue by added the parent controls delegate methods to the child controls. The problem remains that the click event also is ignored over the child controls when I've subscribed to the click event on my parent control. I can subscribe to each individual child control, but how do I force the click event of the parent control? The term I've found by searching is Event Bubbling, but this seems to only apply to ASP.NET technologies and frameworks. Any suggestions?

推荐答案

您的描述使我相信,你想同时你的孩子和你的父母控件上的子控件的点击响应。

Your description leads me to believe that you want both your child and your parent controls to respond to a click on the child control.

如果我正确地理解你的问题,我建议你订阅你的孩子控件的点击事件,在这些事件处理程序,调用该操纵在你想要的方式父用户控件的状态,一些常用的方法(例如,改变背景颜色)。

If I understand your question correctly, I'd suggest subscribing to your child controls' click events and, in those event handlers, calling some common method that manipulates the state of the parent UserControl in the manner that you desire (e.g., changing the background color).

这篇关于C#的WinForms用户控件鼠标事件帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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