C# – Calling public method of user control from a parent form

cc#-4.0user-controlswinforms

I have form containing a Clear Button, usercontrol. The userControl has some text box and labels.

On the click of clear button the entries in the textbox should get cleared.

I have written a public method in userControl class which clears entries on from text box.

How to call this clear() method from clicking clear button from parent form?

Best Answer

your Clear() method should be something like this

//this method in the userControl
public void Clear()
{
   //Clear your text box
   this.txtbox1.Text = string.Empty;
   //Do other clean-up things if you want
}

now in your parent form, use your userControl name (you must add the userControl to your parent form) and call this code upon clicking the button event

private void button1_Click(object sender, EventArgs e)
{
   //Call the Clear method from the UserControl
   yourUserControlName.Clear();
}

otherwise, please share your code.