简体   繁体   English

从另一个静态类访问表单方法

[英]Access Form Method from another static class

So, I'm pretty much out of clues by now, not even sure if it's possible after all. 所以,我现在几乎没有线索,甚至不确定它是否可能。 I have a Visual C# Form, which gets run by the Program.cs (Standard way - VS did all the setup work of course). 我有一个Visual C#Form,它由Program.cs运行(标准方式 - 当然VS完成了所有设置工作)。

In addition to that, I have a class with a static method in a seperate C# file, just because I like keeping one class in one file. 除此之外,我在一个单独的C#文件中有一个带静态方法的类,只是因为我喜欢将一个类保存在一个文件中。

My form code has a public function: 我的表单代码有一个公共函数:

public void print(String text)
{
    rtb_log.appendText("\n" + text);
}

At a certain point of time, I'm calling the static function from the other class. 在某个时间点,我从另一个类调用静态函数。

Is it possible, to actually access that print method from my other class? 是否有可能从我的其他课程实际访问该打印方法? Since it's referring to rtb_log ( a rich text box), it's only availible if instanced, and of course not static. 因为它指的是rtb_log(一个富文本框),所以只有实例化才有效,当然也不是静态的。 But since static methods can only access static members, I'm a little out of ideas here on how to append some text on my form from another class. 但是因为静态方法只能访问静态成员,所以我在这里有点想法如何在另一个类的表单上附加一些文本。

Any help here? 这里有什么帮助?

But since static methods can only access static members, I'm a little out of ideas here on how to append some text on my form from another class. 但是因为静态方法只能访问静态成员,所以我在这里有点想法如何在另一个类的表单上附加一些文本。

Static members can access instance members - they just need to know which instance to call the method on. 静态成员可以访问实例成员 - 他们只需要知道调用该方法的实例。 So you could write: 所以你可以写:

public static void Foo(OtherForm myOtherForm)
{
    // Do some stuff...
    myOtherForm.Print(); // Case changed to comply with naming conventions
}

Then when you call the method, you need to supply a reference to the relevant form. 然后,当您调用该方法时,您需要提供相关表单的引用。 Basically something has to determine which instance you want to call Print on. 基本上东西已经确定要调用的实例Print上。 Work out who has that information, and pass it on from there. 弄清楚谁拥有这些信息,然后从那里传递信息。 I would recommend against using a static variable to hold this information. 我建议不要使用静态变量来保存这些信息。 (Global state makes code less reusable, harder to reason about, and harder to test.) (全局州使代码更少可重用,更难以推理,并且更难测试。)

EDIT: Given the comments, it sounds like you want: 编辑:鉴于评论,这听起来像你想要的:

// Within the form
private void HandleClick(object sender, EventArgs e)
{
    SomeClass.StaticMethod(this);
}

See below 见下文

class SomeMainClass
{
    private ClassB form = null;

    private void SomeMethod()
    {
        form = new ClassB();
        form.Show();
        ClassA foo = new ClassA(this);
    }

    // Use an accessor.
    public ClassB Form
    {
        get { return this.form; }
    }
}

class ClassA
{
    private SomeMainClass mainClass = null;

    // Constructor.
    public ClassA(SomeMainClass _mainClass)
    {
        this.mainClass = _mainClass;
    }

    private void SomeMethod()
    {
        this.mainClass.Form.Print("Something to print");
    }
}

class ClassB : Form
{
    // Constructor.
    public ClassB()
    {
        InitializeComponent();
    }

    public void Print(String text) 
    {     
        rtb_log.appendText("\n" + text); 
    } 
}   

Edit: This is a basic methodology in response to your comment. 编辑:这是回应您的评论的基本方法。 It is not that efficent in terms of resources but does what you want... 在资源方面不是那么高效,而是做你想要的......

I hope this helps. 我希望这有帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM