以另一种形式访问函数 - C# [英] Accessing a function in another form -- C#
问题描述
我有这个战舰计划,我试图以另一种形式访问一个功能。此功能用于将游戏板上的按钮更改为启用或禁用,具体取决于轮到谁。将按钮更改为启用和禁用不是我的问题。我的问题是访问该功能,以另一种形式做到这一点。我会发布代码,但它很长,并且在三种形式之间传播。
感谢任何帮助!
谢谢
Luke
这取决于您的代码架构:
-
粗糙的:如果您只有几种形式
表单_form1,表单2 _form2
,您可以在它们之间创建一种关系,仅仅是一个伪代码示例:
public class Form1:Form
{
Form2 _form2Object = null;
public Form1(Form2 frm2)
{
_form2Object = frm2;
}
//在需要时使用_form2Object调用一个mehod。
-
更好的:是declare所有表单事件调度程序之间共享。因此,当
Form1
想要通知某些对象Form2
时,它会调用Dispatchers相关方法, code> Form2
可能有更多其他解决方案不那么好,但它强烈依赖于您的应用程序体系结构。在这里,我只是放下了你可以选择的一些选择。
希望这有帮助。
问候。
I have this battleship program where i am trying to access a function in another form. This function is used to change the buttons on the game board to either enabled or disabled, depending on who's turn it is. Changing the buttons to enabled and disabled is not my problem. My problem is accessing the function to do it in the other form. I would post code, but it is lengthy and spread between three forms.
Appreciate any help!
Thanks in advance!
Luke
It depends on your code architecture:
Kind of rough one: If you just have several forms
Form _form1, Form2 _form2
, you can create kind of relationship between them, by, just an example pseudocode:public class Form1:Form { Form2 _form2Object = null; public Form1(Form2 frm2) { _form2Object = frm2; } //and after when needed just use that _form2Object to call a mehod on it. }
More nice one: is declare shared between all your forms event Dispatcher. So when
Form1
wants to notify somethign toForm2
it calls Dispatchers relative method, which takes care to call right method onForm2
There could be a lot of other solutions more or less nicer, but it strongly depends on your app architecture. Here I just put down a couple of choices you could have.
Hope this helps. Regards.
这篇关于以另一种形式访问函数 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!