I've been searching the net to try to find solution but i still can't find it despite searching it for hours.
I've got a user control in a form. From the user control, I plan to do 2 things.

1) In the parent form, i got function example FunctionX(). I would like to call FunctionX() from user control.
2) In the parent form, I got buttonclick event. I would like to trigger the event from user control.

The closest I answer i get is delegate. But i still have no clear answer as to how to do it.
Please point me to the right direction.

5 Years
Discussion Span
Last Post by mrnobody

If I understand correctly: You want to call a function and trigger an event at the same time from a user control.

If that's correct (and you're using Visual Studio):
Double-click the user control in design mode. It will create the button_click handler. If you already have that, put the call to your function inside the button_click handler.

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {

I would do that if both the function and caller is in the same form.
But in this case, it is not.
Sorry for not word it correctly in my first post.

My function "FunctionX" is in the parent form.
I would like to call it from user control.

I have read somewhere that I can pass the parent reference to the user control when I instantiate the user control (using constructor) but that is against the principle of "separation of concern".

The better way is, when user click a button in user control, i raise an event which is caught/handled by the parent form, which in turn call the parent's FunctionX.

After hours of searching through the internet, that's the furthest I've got. I still do not know how to actually do it.
Hope somebody can give me a clear example.


Can you make the function public and static inside the parent?
- still calling it using the method above adding the parent class name to it "{parent}.FunctionX()"

If not, can you pass it to the child in the constructor (a delegate)?
If you need an example, I'll create one.


OK. I made a plain form with 1 button on it.
I added a static method to the form:

static String^ FunctionX(String^ strData)
      return "hey-" + strData + "-there";

I then added a button-click event handler to the button "button1_Click()"

// sets the form title and the button title
private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {
   this->Text = Form1::FunctionX("asdf");
   button1->Text = this->Text;

Which code is for the parent form and which code is for the user control?
How do I pass the parent's reference to the user control's constructor?
I need the parent's reference in order for the user control to call the parent's function right?


The form is the parent and the button is the child.
The child then calls the static function in the parent (the form).
Since the FunctionX() is public and static, anything in scope can call it.

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.