C # WinForms UserControl Mouse Event Help

I have a custom control that I created for my project. There are several child controls in this control, such as Label, PictureBox, and LinkLabel. Other than LinkLabel, I want the mouse over the event to currently be on the parent control and for the control to respond to the mouse. The background color changes when you hover over a control, but the background color does not change when above a child control; this is because there are no MouseEnter and MouseLeave events on the child control. I solved this problem by adding delegate parent controls to child controls. The problem remains that the click event is also ignored over child controls when I subscribed to the click event on my parent control.I can subscribe to each individual child control, but how can I fire the click event for the parent control? The term I found by searching is the Bubbling event, but it seems to only apply to ASP.NET technologies and infrastructures. Any suggestions?

+3
source share
4 answers

Your description makes me think that you want both your child and your parent control to respond to a click on a child control.

If I understood your question correctly, I would suggest subscribing to the click events of your child controls and in these event handlers call the usual method that controls the state of the parent UserControl in the way you want (for example, changing the background color).

+2
source

It seems someone else has decided this for me. A friend explained that C # controls support the InvokeOnClick (Control, EventArgs) method;

, 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;
}
+1

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

, , ​​ .

CustomMethods.RelayEvents(Me, New EventHandler(AddressOf Me_Click))
+1
0

Source: https://habr.com/ru/post/1698008/


All Articles