如何在已经打开的表单上调用C#中的接口方法? [英] How do you call an interface method in C# on a form which is already open?

查看:78
本文介绍了如何在已经打开的表单上调用C#中的接口方法?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好 - 我有一个关于如何在已经打开的表单中使用界面的问题。



我有一个C#项目,里面有两个表单,A和B.



我在一个单独的类中有一个接口X,B实现了接口方法,它更新了B中的某些文本框。



我的问题是:如何在不创建B的新实例的情况下从A调用界面?



例如,如果我在A中这样做:



Hi all - I have a question regarding how to use an interface in a form that is already open.

I have a C# project with two forms in it, A and B.

I have an interface X in a separate class, and B implements the interface methods, which update certain text boxes in B.

My question is: how do I call the interface from A without creating a new instance of B?

For example, if I do this in A:

frmB xxx = new frmB();
xxx.PerformInterfaceMethod();
xxx.Show();





然后当然会弹出一个新的B实例,其中包含PerformInterfaceMethod定义的动作()导致它的一些文本框被修改,但这不是我想要的。



原因是B已经开放了一些信息。你按一个按钮,导致A打开,然后你在A中选择一些会导致B改变其某些文本框内容的东西,所以我不想创建一个新的B实例:我想影响B的实例已经打开。



我该怎么做?



then of course a new instance of B pops up, with the actions defined by PerformInterfaceMethod() causing some of its text boxes to be modified, but this is not what I want.

The reason is that B is already open with some information on it. You press a button, causing A to open, then you select something in A which will cause B to alter the content of some of its text boxes, so I don''t want to create a new instance of B: I want to affect the instance of B that is already open.

How do I do this?

推荐答案

拿一个FormB中的静态变量,它将保存当前打开的表单的引用:



FormB中的代码:

Take a static variable in FormB which will hold the reference of currently opened form:

Code in FormB:
public class FormB : .... //interfaces from which you are deriving
{
    public static FormB frmCurrentInstance;

    public FormB(){
        frmCurrentInstance = this;
    }
    .........
    .........
}





在FormA中,通过使用FormB的静态变量,您可以访问FormB的当前实例,并使用该实例可以调用该方法来执行所需的操作。



使用FormB的静态变量访问PerformInterfaceMethod()。



In FormA by using the static variable of FormB you can access the current instance of FormB, and using that instance you can invoke the method to perform desired action.

Access the PerformInterfaceMethod() using static variable of FormB.

FormB.frmCurrentInstance.PerformInterfaceMethod();





希望这能解决您的要求。



Hope this solves your requirement.


您好,

没有理由不能实施两个类的接口。当你想到界面时,你会想到可以做或可以拥有这个词,例如动物可以走路,所以步行将是一个界面IWalk;所以给定的动物例如狗可以实现IWalk但蛇不会。



因此在代码中:

Hi,
There is no reason why you cannot implement the interface on both classes. when you think of interface, you think of the word "can do" or "can have" for example an animal can walk so walk will be an interface "IWalk"; so a given animal for example dog can implement the IWalk but snake will not.

So in code:
public interface IWalk
{
    int NumberOfLegs(int legs);
}

public interface ISwim
{
    int Speed();
}

public class Dog : IWalk, ISwim
{
    public Dog()
    {
    }

    private int NumberOfLegs(int legs)
    {
         // do something with the legs
    }

    private int Speed()
    {
         // how fast dog can swim
    }
} 

public class Cat : IWalk
{
    public Cat()
    {
    }

    private int NumberOfLegs(int legs)
    {
         // do something with the legs
    }
} 

public class Snake : ISwim
{
    public Skane()
    {
    }

    private int Speed()
    {
         // how fast snake can swim
    }
}

public class Animal
{
    private Snake _snake;
    public Animal()
    {
        _snake = new Snake();
    }

    private void GetSnakeSwimSpeed()
    {
        int snakeSwimSpeed = _snake.Speed();
    }
}





您看到了,您可以实现所有必需对象的界面。 />


我希望这会有所帮助。



问候

Jegan



Did you see, you can implement an interface to all your required objects.

I hope this helps.

Regards
Jegan


这篇关于如何在已经打开的表单上调用C#中的接口方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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