获取对申请家长表格的引用 [英] Getting a Reference to Application Parent Form

查看:100
本文介绍了获取对申请家长表格的引用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有人知道有关如何实现以下目标的任何标准或白皮书:

我有一个项目,该项目具有一个名为myForm的表单和一个名为ClassA的类.在ClassA中,我有一个名为CreateButton的方法,该方法通过代码创建Button类的实例.问题是:由于ClassA没有对myForm的引用,如何将这个Button对象添加到Controls集合中?
我知道我可以将对myForm的引用传递给ClassA的构造函数,但我不希望此类对myForm有所了解.基本上,我不希望该类与表单一起使用.

我知道我可以使用委托来完成此操作,但是如果我的类创建其他类型的控件,则必须为每种类型都有一个委托,这绝对不是一个好的解决方案.

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?

推荐答案

0)您可以通过构造函数将对表单的引用传递到ClassA中.

1)您可以设置一个全局静态类,其中包含对表单的(公共)引用,并仅使用ClassA中的引用即可.
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.


这篇关于获取对申请家长表格的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆