简体   繁体   English

C#-从另一个类写入Form变量

[英]C# - Writing to Form variable from another class

In brief, One class is responsible for initialization and main GUI controls, while another does all the requests, data collection and returns the results which can take a bit of time to be completely finished. 简而言之,一类负责初始化和主GUI控件,而另一类负责所有请求,数据收集并返回结果,这可能需要一点时间才能完全完成。

I have no problems passing data from 2nd to gui class (let's call this a main class) once it's all been collected by using public vars. 一旦使用公共变量将数据从第二类传递到gui类(我们称其为主要类),我就没有问题。 However, writing directly to the form from 2nd class doesn't seem to be so straight forward. 但是,直接从第二类编写表格似乎并不那么直接。

What I tried to do was in the 2nd class make a instance of the main class, then inside a method of 2nd class repeatedly call the public method from the main class which will write the data directly to the form. 我试图做的是在第二类中创建主类的实例,然后在第二类的方法内部反复从主类调用public方法,该方法将数据直接写入表单。 That was the theory at least. 至少这是理论。

So it looks like this: 所以看起来像这样:

2nd class: 二等班:

Gui r1 = new Gui(); 
// ...
result += someVar + "\n";
// ...
r1.setResultsInfo(result);


// method inside the main class for writing to form:

public partial class Gui : Form
{
    public void setResultsInfo(String var)
    {
        resultsInfo.Text += var;  
    }
}

However, this doesn't write anything to the Form. 但是,这不会向表单写入任何内容。 Accessing and writing to resultsInfo directly from the main class works fine, just not from the 2nd class through an instance of the main class. 直接从主类访问和写入resultsInfo可以正常工作,只是不能从第二类通过主类的实例进行访问。 I guess it could be because it's trying to write to some another instance of not really existent r1 form, but how I'am supposed to access my active form if I obviously can't access the main class without creating an instance of it? 我猜可能是因为它试图写另一个不存在的r1形式的实例,但是如果我显然不能不创建它的实例而无法访问主类,那么我应该如何访问我的活动形式呢?

Updated Code with more details: 更新的代码具有更多详细信息:

public partial class Gui : Form
    {

        public Gui Instance;

        public Gui()
        {
            Instance = this;
            InitializeComponent();

        }


// 2nd class: 


static class Program
{


    public static QueryResult[] arr = new QueryResult[20];
    public static string result;

    [STAThread]





    private static void QueryAPIAndPrintResult(string term, string location)
    {

        Gui r1 = new Gui();

        string var;


        System.Console.WriteLine(var = "Ieškoma: '" + term + "' \nvietovėje: '" + location + "'");


        result += var + "\n";

        r1.Instance.setResultsInfo(var); 
        // Gui.Instance.setResultsInfo() // throws error about requiring an instance. 
        // ... 
    }

        // ...
} 


//  main method: 


 static void Main(string[] args)
    {
        Application.EnableVisualStyles();
        Application.SetCompatibleTextRenderingDefault(false);
        Application.Run(new Gui());
    }

Where are you trying to look for the written data? 您要在哪里寻找书面数据? You are creating a new instance of Gui and, as per the code you posted, you are never even showing that Form. 您正在创建一个Gui的新实例,并且按照您发布的代码,甚至从未显示该Form。

I would suggest you to take a look at the Singleton Pattern , and do something like this: 我建议您看一下Singleton Pattern ,然后做类似的事情:

public class Gui
{
    public static Gui Instance;

    Gui()
    {
        // constructor stuff
        Instance = this;
    }
}

// from your other class
// ...
result += someVar + "\n";
// ...
Gui.Instance.setResultsInfo(result);

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

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