首页 文章

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

提问于
浏览
1

我正在努力在C#中传递一个Variable(一个字符串)来解决一个特殊问题:

Overview:

我正在为我公司购买的程序编写一个插件 . 程序(或更好:程序支持)为用户提供基本的C#-Code,它基本上只是打开一个表单,并将程序与我在表单代码中写下的内容连接起来 . 由于它是一个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
    }
}

现在,正如我在代码部分中标记的那样,我的整个代码在“click-compute”函数中运行,并且“外包”到其他类中 .

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

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

Problem:

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

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

What I already tried:

1.)我试图将“MyUserInterface”中的所有内容基本上改为“公共”,

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,但它们都有共同点,即“变量”已知之后“显示形式”是开启的 . 在我的特殊情况下,表格一直打开,不能关闭,也不能重新打开......

而现在我的想法!我不会感到惊讶,如果我没有看到一些细节,但我找不到它们...当有人能帮助我时,我会很高兴!

My question:

如何从另一堂课改变我的文字?

2 回答

  • 0

    在第一个表单中的特定类上创建一个公共属性,获取标签的值,如下所示:

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

    然后在你的第二个表格中:

    public Form2(Form1 form)
    {
        string name;
        name = form.Name;
    }
    
  • 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 / Disposed表格等 .

相关问题