C#WinForms UserControl鼠标事件帮助

时间:2021-04-14 00:01:43

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?

我有一个为我的项目创建的自定义控件。在此控件中有几个子控件,如Label,PictureBox和LinkLabel。除了LinkLabel之外,我希望当前在父控件上的鼠标悬停事件并让控件响应鼠标。将鼠标悬停在控件上时背景颜色会发生变化,但是当通过子控件时背景颜色不会改变;这是因为子控件上没有MouseEnter和MouseLeave事件。我通过将父控件委托方法添加到子控件来解决了这个问题。问题仍然是当我订阅了我的父控件上的click事件时,click控件上的click事件也会被忽略。我可以订阅每个单独的子控件,但是如何强制父控件的click事件?我通过搜索找到的术语是Event Bubbling,但这似乎只适用于ASP.NET技术和框架。有什么建议?

4 个解决方案

#1


2  

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).

如果我正确理解你的问题,我建议订阅你的子控件的click事件,并在那些事件处理程序中调用一些常用的方法来以你想要的方式操纵父UserControl的状态(例如,改变背景颜色)。

#2


1  

It seems that someone else solved it for me. A friend explained that C# controls support a method called InvokeOnClick(Control, EventArgs);

似乎其他人为我解决了这个问题。一位朋友解释说C#控件支持一个名为InvokeOnClick的方法(Control,EventArgs);

I added my event delegate methods to each child control, and for the click event, I created a single method for each child control to use. This in turn calls InvokeOnClick(this, new EventArgs());

我将事件委托方法添加到每个子控件,对于click事件,我为每个子控件创建了一个单独的方法。这反过来调用InvokeOnClick(这个,新的EventArgs());


private void Control_Click(object sender, EventArgs e)
{
    // this is the parent control.
    InvokeOnClick(this, new EventArgs());
}

private void IFLVControl_MouseEnter(object sender, EventArgs e)
{
    this.BackColor = Color.DarkGray;
}

private void IFLVControl_MouseLeave(object sender, EventArgs e)
{
    this.BackColor = Color.White;
}

#3


1  

I had a similar problem with handling the Click event in all child controls. The code is VB.NET but it should be easily adjustable.

我在处理所有子控件中的Click事件时遇到了类似的问题。代码是VB.NET,但它应该很容易调整。

Public Shared Sub RelayEvents(ByVal usrcon As Windows.Forms.Control, ByVal del As System.EventHandler, Optional ByVal includeChildren As Boolean = True)
    For Each con As Windows.Forms.Control In usrcon.Controls
        AddHandler con.Click, del
        If includeChildren Then
            RelayEvents(con, del)
        End If
    Next
End Sub

Whenever the desired cascading is needed the following line can be added to the constructor of the class.

每当需要所需的级联时,可以将以下行添加到类的构造函数中。

CustomMethods.RelayEvents(Me, New EventHandler(AddressOf Me_Click))

#4


0  

Peter Rilling over at CodeProject has some simple and effective code to do event bubbling/broadcasting in winforms (and C#). It's really easy to use.

Peter Rilling在CodeProject上有一些简单有效的代码,用于在winforms(和C#)中进行事件冒泡/广播。它真的很容易使用。

http://www.codeproject.com/KB/cs/event_broadcast.aspx

#1


2  

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).

如果我正确理解你的问题,我建议订阅你的子控件的click事件,并在那些事件处理程序中调用一些常用的方法来以你想要的方式操纵父UserControl的状态(例如,改变背景颜色)。

#2


1  

It seems that someone else solved it for me. A friend explained that C# controls support a method called InvokeOnClick(Control, EventArgs);

似乎其他人为我解决了这个问题。一位朋友解释说C#控件支持一个名为InvokeOnClick的方法(Control,EventArgs);

I added my event delegate methods to each child control, and for the click event, I created a single method for each child control to use. This in turn calls InvokeOnClick(this, new EventArgs());

我将事件委托方法添加到每个子控件,对于click事件,我为每个子控件创建了一个单独的方法。这反过来调用InvokeOnClick(这个,新的EventArgs());


private void Control_Click(object sender, EventArgs e)
{
    // this is the parent control.
    InvokeOnClick(this, new EventArgs());
}

private void IFLVControl_MouseEnter(object sender, EventArgs e)
{
    this.BackColor = Color.DarkGray;
}

private void IFLVControl_MouseLeave(object sender, EventArgs e)
{
    this.BackColor = Color.White;
}

#3


1  

I had a similar problem with handling the Click event in all child controls. The code is VB.NET but it should be easily adjustable.

我在处理所有子控件中的Click事件时遇到了类似的问题。代码是VB.NET,但它应该很容易调整。

Public Shared Sub RelayEvents(ByVal usrcon As Windows.Forms.Control, ByVal del As System.EventHandler, Optional ByVal includeChildren As Boolean = True)
    For Each con As Windows.Forms.Control In usrcon.Controls
        AddHandler con.Click, del
        If includeChildren Then
            RelayEvents(con, del)
        End If
    Next
End Sub

Whenever the desired cascading is needed the following line can be added to the constructor of the class.

每当需要所需的级联时,可以将以下行添加到类的构造函数中。

CustomMethods.RelayEvents(Me, New EventHandler(AddressOf Me_Click))

#4


0  

Peter Rilling over at CodeProject has some simple and effective code to do event bubbling/broadcasting in winforms (and C#). It's really easy to use.

Peter Rilling在CodeProject上有一些简单有效的代码,用于在winforms(和C#)中进行事件冒泡/广播。它真的很容易使用。

http://www.codeproject.com/KB/cs/event_broadcast.aspx