如何获得对另一种形式的引用? [英] How get a reference to another form?
本文介绍了如何获得对另一种形式的引用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
表格B需要调用表格A的公共方法。表格B如何使用其名称或任何其他形式获得表格A实例的
引用
方法?是否有可以搜索的表单实例集合?
-
Bill
解决方案
< blockquote>你需要声明并实例化一个新的A实例并从那里引用它
//在formB中
formA f = new formA();
f.SomeMethod();
" Bill Todd" < no@no.com>在消息中写道
news:le ******************************** @ 4ax.com ...
表单B需要调用表单A的公共方法。表单B如何使用其名称或任何其他方式获得对表单A实例的引用
方法?是否有可以搜索的表单实例的集合?
-
Bill
创建新实例将不起作用。我需要访问表单A的现有
实例。
我从你的答案中假设.NET框架没有维护
表单实例的集合。
我习惯于一个引用一个表单实例的环境
表单另一个很容易,我很难理解表单是如何形成的?b $ b应该在.NET环境中相互通信。
现在我能看到的唯一方法就是拥有
应用程序的主窗体负责在应用程序中创建每隔一个
无模式窗体。当主窗体创建一个新的
表单实例时,它通过调用静态方法来实现,该方法将主窗体的
引用作为参数并返回对<的引用br />
新表格。这样所有表单都可以引用主表单和
主表单引用所有其他表单。
有没有更好的方法来处理这个?
只有一个例子是数据库应用程序的情况。你不能写
写一个数据库应用程序,其中每个表单都有自己的连接
到数据库。如果表单要共享一个连接对象,他们必须要有一个包含连接的表单的引用
对象。
感谢您对此有任何见解。
-
比尔
如果一个表单创建另一个表单,将第一个表单的引用传递给
第二个表单的构造函数...即formA是第一个表单并且它
创建formB:
FormB formB = new FormB(this);
并为FormB创建一个构造函数:
Public FormB(FormA formA)
{
}
HTH,
Dale
" Bill Todd" < no@no.com>在留言中写道
新闻:js ******************************** @ 4ax.com ...创建新实例将不起作用。我需要访问表单A的现有
实例。
我从你的答案中假设.NET框架没有维护表单实例的集合。
我习惯于一个引用一个表单实例的环境
形成另一个表单实例很容易,我很难理解表单是如何在.NET环境中相互通信的。
现在我能看到的唯一方法就是让
应用程序的主窗体负责在应用程序中创建其他所有无模式窗体。当主窗体创建一个新的窗体实例时,它通过调用一个静态方法来实现,该方法将主窗体的引用作为参数,并返回对新窗体的引用。这样所有表单都可以引用主表单,而主表单引用所有其他表单。
有没有更好的方法来处理这个?
感谢您对此有任何了解。
-
Bill
Form B needs to call a public method of Form A. How can form B get a
reference to the instance of Form A using its name or any other
method? Is there a collection of form instances that can be searched?
--
Bill
解决方案
You''ll need to declare and instantiate a new instance of A and reference it
from there
//Within formB
formA f = new formA();
f.SomeMethod();
"Bill Todd" <no@no.com> wrote in message
news:le********************************@4ax.com...Form B needs to call a public method of Form A. How can form B get a
reference to the instance of Form A using its name or any other
method? Is there a collection of form instances that can be searched?
--
Bill
Creating a new Instance will not work. I need access to the existing
instance of Form A.
I assume from your answer that the .NET framework does not maintain a
collection of form instances.
I am accustomed to an environment where referring to one form instance
form another is easy and I am struggling to understand how forms are
supposed to communicate with each other in the .NET environment.
Right now the only method I can see to do this is to have the
application''s main form be responsible for creating every other
modeless form in the application. When the main form creates a new
form instance it does so by calling a static method that takes a
reference to the main form as a parameter and returns a reference to
the new form. That way all forms have a reference to the main form and
the main form has a reference to all other forms.
Is there a better way to handle this?
Just one example is the case of a database applicaiton. You cannot
write a database application where every form has its own connection
to the database. If forms are going to share a connection object they
have to have a reference to the form that contains the connection
object.
Thanks for any insight into this.
--
Bill
If one form creates the other form, pass a reference to the first form to
the constructor of the second form... i.e. formA is the first form and it
creates formB with:
FormB formB = new FormB(this);
and create a constructor for FormB as:
Public FormB(FormA formA)
{
}
HTH,
Dale
"Bill Todd" <no@no.com> wrote in message
news:js********************************@4ax.com...Creating a new Instance will not work. I need access to the existing
instance of Form A.
I assume from your answer that the .NET framework does not maintain a
collection of form instances.
I am accustomed to an environment where referring to one form instance
form another is easy and I am struggling to understand how forms are
supposed to communicate with each other in the .NET environment.
Right now the only method I can see to do this is to have the
application''s main form be responsible for creating every other
modeless form in the application. When the main form creates a new
form instance it does so by calling a static method that takes a
reference to the main form as a parameter and returns a reference to
the new form. That way all forms have a reference to the main form and
the main form has a reference to all other forms.
Is there a better way to handle this?
Just one example is the case of a database applicaiton. You cannot
write a database application where every form has its own connection
to the database. If forms are going to share a connection object they
have to have a reference to the form that contains the connection
object.
Thanks for any insight into this.
--
Bill
这篇关于如何获得对另一种形式的引用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文