Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows Forms: User controls and events

I have a Windows Forms user control which consists of several controls. Among others there is also gridview control. Now I want to use the user control in a form. How can I check for example if a grid row is clicked? I want to get the data of the selected row. In other words, how can I detect events of the controls, which are embedded in the user control?

like image 881
Elmex Avatar asked Feb 27 '23 03:02

Elmex


1 Answers

You need to expose the events from your parent control by adding additional events:

public event EventHandler GridViewClicked;

And you call it on your child control using the following:

private void ChildControlGrid_RowClicked(object sender, EventArgs e)
{
    if (GridViewClicked != null)
    {
        GridViewClicked(sender, e);
    }
}

You then implement this the same way you would any event on your form:

yourControl.GridViewClicked += new EventHandler(ChildGridRowClicked);

private void ChildGridRowClicked(object sender, EventArgs e)
{
    // Child row clicked
}
like image 58
djdd87 Avatar answered Mar 04 '23 10:03

djdd87