Click here to Skip to main content
15,920,896 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Does any one know of any standards or white papers on how to accomplish the following:

I have a project with a form called myForm, and a single class called ClassA. Within ClassA I have a method called CreateButton, which creates an instance of Button Class via code. The question is: how do I add this Button object to the Controls collection since the ClassA does not have a reference to myForm?

I know that I could pass a reference to myForm to the constructor of ClassA, but I do not want this class to know anything about myForm. Basically I do not want that class to be coupled with the form.

I know I could use a delegate to accomplish this, but if my class creates other types of controls it would have to have a delegate for each type, which is definetly not a good solution.

Does any one knows what is the right way of doing something like this?
Posted

1 solution

0) You could pass a reference to the form into ClassA via the constructor.

1) You could setup a global static class that holds a (public) reference to the form and just use that from ClassA.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900