从不同的形式调用方法。 [英] Call a method from different form.

查看:62
本文介绍了从不同的形式调用方法。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的所有人,



只是想知道你是否有可能从表格2中的按钮调用方法,因为我们正在交叉 -thread调用windows窗体控件错误在窗体2中触发按钮后。



在我的程序中,它有两个窗体,窗体1是主窗体,它将包含此处的所有主要方法,例如,open com port()和close com port()方法。所有主要方法都在这里的原因是因为表格1将是最后一个要关闭的表格。



如果这些方法位于表格2中,那么关闭表格2后会出现问题,所有的申请或方法都将被停止和关闭,



因此,我们需要主表格表格1方法,并从表单1调用表单2.在表单2中,也许我们可以配置com端口设置并调用com端口方法,比如Open com port方法以表单1启动应用程序然后我们关闭表单2。 />


返回按摩和com端口状态将显示在表格1上。



请告知是否可以从2调用该方法,或者请为此建议任何替代解决方案。



提前致谢。

hcchin

解决方案

通过事物的声音,你和我一样困惑自己!



为什么把所有的com端口放在一个表单中?创建一个类并将其放在那里 - 这样你就不需要开始玩其他形式 - 这是一个不好的想法。



我会有第二个表单创建,配置等等comport类实例,当它准备通过第二个表单中的属性,或通过属性和事件,将其传递回第一个表单,具体取决于您的应用程序的方式应该工作。



但是让Form2调用Form1方法并更新它的控件是一个非常讨厌的方法,因为一堆原因做事!

为什么不只是使用一个类,只是以将使用所有这些方法的形式引用它?






使用最佳编码方法。不要在应用程序的表单/页面中包含所有内容。创建类并在任何需要的地方使用它们。



创建一个具有打开和关闭端口的方法的类,并通过引用此类的对象来调用这些方法多种形式。


Dear All,

Just want to check you all that whether is it possible to call a method from a button in form 2 as we are having a "cross-thread call to windows forms controls error" after fired the button in form 2.

In my program, It has two form, form 1 is the main form, it will contains all the main methods here, example, open com port() and close com port() method. the reason why all the main methods are located here is because form 1 will be the last form to be closed.

If those methods are located in form 2, there will be an issue after closing the form 2, all the application or method will be stopped and closed,

Therefore, we need the main form "form 1" having all the methods, and call the form 2 from form 1. In form 2, perhaps we can configure com port settings and call the com port methods, like Open com port methods to start the application in form 1 then we close the form 2.

The the return massage and status of the com port will be display on the form 1.

Please advise whether possible to call that method from 2, or please suggest any alternative solution for this.

Thanks in advance.
hcchin

解决方案

By the sounds of things, you are confusing yourself as well as me!

Why put all the com port stuff in a form? Create a class and put it in there - that way you don't need to start playing with other forms - which is a poor idea at best.

I would have the second form create, configure, and so forth the comport class instance, as pass it back to the first form when it is ready to go via a property in the second form, or via a property and an event, depending on how your app is supposed to work.

But getting Form2 to call Form1 methods and update it's controls is a very nasty way to do things for a whole bunch of reasons!


Why not just use a class and just reference it in the form that will use all those methods?


Hi,

Use the best practice of coding. do not include everything on form/pages in your application. create classes and use those wherever it is required.

Create a class which have methods to Open and closing of ports and call these methods by referencing the object of this class in multiple forms.


这篇关于从不同的形式调用方法。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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