我可以在另一种形式中使用一种形式的功能吗 [英] can i use function of one form in another form

查看:95
本文介绍了我可以在另一种形式中使用一种形式的功能吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C#.NET,SQL SERVER制作桌面应用程序.
我已经创建了两种形式,并且我希望当一种方法以第二种形式调用时,以一种形式定义的方法应该以第二种形式工作.

请在这方面帮助我.

在此先感谢.

I am making desktop application using C#.NET,SQL SERVER.
I have created two forms and i want that the method defined in one form should work in 2nd form, when that method called in 2nd form.

Please help me in this regard.

Thanks in Advance.

推荐答案

最健壮和安全的方法是通过表单实现和接口.
请查看我过去的解决方案:
如何在列表框之间复制所有项目两种形式 [ ^ ]

—SA
Most robust and safe approach is implementing and interface by the form.
Please see my past solution:
How to copy all the items between listboxes in two forms[^]

—SA


是的.公开该方法(或更好的内部方法).在其他表单代码中,创建第一个类的实例,然后使用该实例调用该方法.


这是基本的事情.我建议您先读一本好书,并花一些时间研究它.
Yes you can. Make that method public (or better internal). In the other form code, create an instance of the first class and call that method using the instance.


That''s a basic thing. I suggest you to get a good beginner book and invest some time studying it.


在Form1中将该功能用作public/internal.
Make that function as public/internal in Form1.
public string CallForm1()
{

}


然后,在Form2中访问下面的函数.


Then, access the function like below in Form2.

Form1 f = new Form1 ();
f.CallForm1();


根据Ankur建议修改


Modified based on Ankur suggestion


这篇关于我可以在另一种形式中使用一种形式的功能吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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