In C# WinForms, I have created a User Control and, for the sake of this example, let's suppose that inside it I have created a button and it trigger an event when I click on it.
I add this custom user control in real-time in my current form. Problem. When I click on the button (which is inside the user control) I don't know how to update the event in my current Form. Anyone know how it can be achieved?
This is the first time I provide an answer, so I hope I'm doing it right. All your other answers were helped but I feel they were vague (probably to a vague question). I tried to provide the solution more clearly as possible. Feel free to improve it if something is wrong, and tell me if it should be marked a 'accepted'. Thanks for your support everyone!
First, in the UserControl I created a button and Button1Click is delegated to it:
this.button1.Location = new System.Drawing.Point(25, 17);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(95, 23);
this.button1.TabIndex = 0;
this.button1.Text = "Click";
this.button1.UseVisualStyleBackColor = true;
this.button1.Click += new System.EventHandler(this.Button1Click);
Later in the source of UserControl:
void Button1Click(object sender, EventArgs e)
{
}
Now, in the MainForm designer, I add a TextBox (for testing the result) and my custom UserControl. On the last line, I watch the event from UserControl and when it's triggered it calls the Button1Click from inside the MainForm:
this.textBox1.Location = new System.Drawing.Point(12, 151);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(202, 20);
this.textBox1.TabIndex = 1;
this.uControl1.Location = new System.Drawing.Point(12, 12);
this.uControl1.Name = "uControl1";
this.uControl1.Size = new System.Drawing.Size(304, 95);
this.uControl1.TabIndex = 0;
this.uControl1.button1.Click += new System.EventHandler(this.Button1Click);
Thus the Text property is changed.
void Button1Click(object sender, EventArgs e)
{
this.textBox1.Text = "Hurrah!";
}