将变量从另一个类传递到表单

时间:2018-12-10 08:02:39

标签: c# winforms parameter-passing

我正努力在C#中传递变量(字符串)以解决特殊问题:

概述:

我正在为公司购买的程序编写插件。该程序(或更好的是:程序支持)为用户提供了基本的C#代码,该代码基本上只是打开一个表单,并将该程序与我在表单代码中写下的内容相连接。 因为它是Visual Studio解决方案,所以我得到了一些文件:“ MyUserInterface.cs”和“ MyUserInterface.Designer.cs”。

“ MyUserInterface.Designer.cs”定义了表单的外观,对于我的问题来说,最重要的部分是:

partial class MyUserInterface
{   
    [...]     
    private void InitializeComponent()
    {
        [...]
        this.f_status = new System.Windows.Forms.Label();
        this.SuspendLayout();
        [...]
        // 
        // status
        // 
        this.f_status.Name = "status";
        this.f_status.Text = "WELCOME TO MYPLUGIN v2";
        [...]
        this.Controls.Add(this.f_status);
        this.ResumeLayout(false);
        this.PerformLayout();
    }

    [...]
    private System.Windows.Forms.Label f_status;
    [...]
}

“ MyUserInterface.cs”中最重要的代码是:

partial class MyUserInterface
{
    [...]
    public MyUserInterface()
    {
        InitializeComponent();
    }
    [...]
    private void click_compute(object sender, EventArgs e)
    {
        //Basically everythings runs here!
        //The code is opend in other classes and other files
    }
}

现在,正如我在代码部分标记的那样,我的整个代码在“点击计算”函数中运行,并“外包”到其他类中。

我的代码的重要部分在“ statushandler.cs”中找到:

class statushandler
{
    [...]
    public static void status_msg(string c_msg)
    {
        [...]
        f_status.Text = c_msg; // And here is my problem!!
        [...]
    }
}

问题:

在我的特殊情况下,我尝试使用“ status_msg”函数在运行代码时更改“ f_status” -Lable的文本!

虽然我在代码中多次在类之间传递变量。一个无法弄清楚,为什么不能在“状态处理程序”中找到此显式变量。 (只要我留在原始的“ click_compute”中,而不必进入其他类,这是没有问题的。)

我已经尝试过的方法:

1。)我试图将“ MyUserInterface”中的所有内容基本上都更改为“ public”,

2。)我也尝试在status_msg中像f_status一样呼叫MyUserInterface.f_status.Text

3。)在“ MyUserInterface。(Designer。)cs”(两者)中编写一个Getter / Setter函数,这是灾难性的,因为我再也无法在InitializeComponent中定义Label。

4。)

a。)阅读大量关于在类之间传递变量的Stackoverflow-Threads,所有这些都无济于事,我发现所有解决方案都在类之间起作用,但在这种特殊情况下不起作用。

b。)观看了许多youTube教程,结果相同。

c。)阅读一些关于在不同的Form之间传递变量的stackoverflow-Thred,但它们都有一个共同点,即在知道变量之后打开“显示表单”。在我的特殊情况下,表单一直打开,无法关闭,也无法重新打开...

现在我没主意了! 如果我看不到一些细节,但我找不到它们,我不会感到惊讶...当有人可以帮助我时,我将非常高兴!

我的问题:

如何从另一个班级更改我的寓言文本?

2 个答案:

答案 0 :(得分:1)

当表单具有实例时,您的方法是静态的。因此,您的静态方法对表单一无所知。您可以将MyUserInterface参数添加到静态方法

public static void status_msg(MyUserInterface form, string c_msg)
{
    [...]
    form.f_status.Text = c_msg; // And here is my problem!!
    [...]
}

如果您具有单个实例形式(一次仅创建一个实例),则可以使用其引用作为静态属性:

partial class MyUserInterface
{
    public static MyUserInterface Instance { get; private set; }
    [...]
    public MyUserInterface()
    {
        InitializeComponent();
        Instance = this;
    }
}

使用此解决方案,您可以使用旧方法:

class statushandler
{
    [...]
    public static void status_msg(string c_msg)
    {
        [...]
        MyUserInterface.Instance.f_status.Text = c_msg; // You have instance of yout form here
        [...]
    }
}

当然,您应该避免使用null /处置形式等。

答案 1 :(得分:0)

在您的1st Form中的特定类上创建一个公共属性,该属性将获得标签的值,如下所示:

public string Name {get {return Label1.Text}; set {Label1.Text = value}; }

然后在您的第二个表格中

public Form2(Form1 form)
{
    string name;
    name = form.Name;
}