以另一种形式访问函数 - C# [英] Accessing a function in another form -- C#

查看:123
本文介绍了以另一种形式访问函数 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个战舰计划,我试图以另一种形式访问一个功能。此功能用于将游戏板上的按钮更改为启用或禁用,具体取决于轮到谁。将按钮更改为启用和禁用不是我的问题。我的问题是访问该功能,以另一种形式做到这一点。我会发布代码,但它很长,并且在三种形式之间传播。

感谢任何帮助!



谢谢



Luke

解决方案

这取决于您的代码架构:


  1. 粗糙的:如果您只有几种形式表单_form1,表单2 _form2 ,您可以在它们之间创建一种关系,仅仅是一个伪代码示例

      public class Form1:Form 
    {
    Form2 _form2Object = null;

    public Form1(Form2 frm2)
    {
    _form2Object = frm2;
    }

    //在需要时使用_form2Object调用一个mehod。


  2. 更好的:是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:

  1. 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.
    }
    

  2. More nice one: is declare shared between all your forms event Dispatcher. So when Form1 wants to notify somethign to Form2 it calls Dispatchers relative method, which takes care to call right method on Form2

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屋!

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