【问题标题】:Passing Variable from another class into a form将变量从另一个类传递到表单中
【发布时间】:2018-12-10 08:02:38
【问题描述】:

我正在努力在 C# 中为一个特殊问题传递一个变量(一个字符串):

概述:

我正在为我公司购买的程序编写插件。该程序(或更好:程序支持)为用户提供了基本的 C# 代码,它基本上只是打开一个表单,并将程序与我在表单代码中写下的任何内容连接起来。 因为它是一个 Visual-Studio-Solution 我得到一些文件:“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 的文本!

虽然我在代码中的类之间传递了几次变量。 A无法弄清楚,为什么在“statushandler”中找不到这个明确的。 (只要我留在原来的“click_compute”内,不进入不同的类,就没有问题)。

我已经尝试过的:

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

2.) 我还尝试在 status_msg 中调用f_status,例如MyUserInterface.f_status.Text,

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

4.)

a.)阅读了很多关于在类之间传递变量的 Stackoverflow-Threads,这些都没有帮助,我发现的所有解决方案都在类之间工作,但在这种特殊情况下不是。

b.)看了很多 YouTube 教程,结果一样。

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

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

我的问题:

如何更改其他类的标签文本?

【问题讨论】:

  • 问题是你的方法是静态的。静态方法在“类”级别工作,并且无法访问实例成员。所以去掉 static 关键字再试一次。
  • 如果您说出它为什么不起作用,它会有所帮助。怎么了?您看到了什么错误?
  • @Isma:试过了:删除“static”并输入“MyUserInterface.f_status.Text = c_msg”给出了明显的错误“'MyUserInterface.f_status'由于其保护级别而无法访问。当然,我的错误,所以我将“InitalizeComponent”和“MyUserInterface.Designer.cs”中的标签声明更改为public。新错误:非静态字段、方法或属性“MyUserInterface.f_status”需要对象引用。 .. AND:由于同样的错误,我无法再在任何地方调用此函数。我会尝试 S.Akbari 写的 solition,如果那不起作用,稍后再返回。
  • @S.Akbari : 链接中的解决方案的问题是,您必须生成一个新的 Inctance 表单,这在此处是不可能的。

标签: c# winforms parameter-passing


【解决方案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
        [...]
    }
}

当然你应该防止空/处置形式等。

【讨论】:

  • 解决方案有效。还有一点:我必须将“f_status”更改为公开,但在这种情况下很清楚。谢谢!
【解决方案2】:

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

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

然后在你的第二种形式中:

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

【讨论】:

  • 虽然不能通过 getter 设置值。需要public String Name {get {return Label1.Text;} set {Label1.Text = value;} };
  • 也许我不明白你的意思,但这不是反过来吗?所以在我的情况下,我只有 1 个表单,所以我在我的 Designer-File 中添加了"public string status_bridge { get { return f_status.Text; } set { f_status.Text = value; } }"。现在,我有我的“statushandler”类,而不是第二种形式,我想在其中设置值,而不是获取它?
猜你喜欢
  • 2013-02-08
  • 1970-01-01
  • 1970-01-01
  • 2015-10-23
  • 1970-01-01
相关资源
最近更新 更多