I have a windows user control "MyControl" which contains a method "MyMethod".
I add the control dynamically to my form which is easy enough but how does my parent form then call the method ?
TIA ;)
Printable View
I have a windows user control "MyControl" which contains a method "MyMethod".
I add the control dynamically to my form which is easy enough but how does my parent form then call the method ?
TIA ;)
The question you've asked doesn't have anything to do with raising events. Have you mis-stated the question?
When you say dynamically I assume that you mean at run time. Just like any object, you must have a reference to this UserControl to access its members. You can assign it to a class-level variable when you create it and access it through that. This variable could be of the same type as the UserControl, or it could be an array or a collection and you add the control as an element or item. Alternatively you can access the control by iterating over the Controls collection of the form until you find a control that matches whatever criteria you have, like a specific Name, Tag or just the type.
Sorry if it sounds strange but to be honest I dont know what the heck Im doing.
Basically when the user clicks on a button on my form I want my user control to be loaded
which is easy enough.Code:"ctlAddProject controlAddProject;Controls.Add(controlAddProject);"
Now there is a public method called doBind(string fName) on the control which I want to call from the parent form passing the appropriate string.
If the control was added at design time I could do something like :
How do I do this now the control is added at run time ?Code:controlAddProject.doBind("StringHere");
Like I said, you need to keep a reference to this object when you create it. How many of these objects will you have? Will it be a set number or will it vary? If there is just one then you can use a single variable. If it's a fixed-number greater than one the use an array. If it's not a fixed number then a collection would be the way to go.