I have a custom control (UserControl) that contains two labels and a button, I load this control dynamically in a form. When I click on the custom control button, I would like to get the information contained for example in the label1 of the custom control.
private void mycustomcontrol_MouseClick(object sender, MouseEventArgs e)
{
if(e.Button == MouseButtons.Left)
{
mycustomcontrol = sender as mycustomcontrol;
MessageBox.Show(mycustomcontrol.Info); // <-- mycustomcontrol.Info refers to the label1 text on the custom control
}
}
In my Usecontrol:
public new event MouseEventHandler MouseClick
{
add
{
button1.MouseClick += value;
}
remove
{
button1.MouseClick -= value;
}
}
I get an exception: System.NullReferenceException: 'Reference to an object not set to an object instance.'