How to change a label from another class? c# windows forms visual studio

后端 未结 4 737
旧巷少年郎
旧巷少年郎 2021-01-28 01:25

I know there are a lot of threads talking about this and believe me I\'ve seen all of them, but I think I\'m a little slow and cant figure out how to do this so here is the thin

4条回答
  •  天命终不由人
    2021-01-28 01:52

    I am also looking for answer, but i finally found out how to change the label of form1 from another class.

    usually Form1.Designer.cs looks like this:

                this.label6.AutoSize = true;
            this.label6.Location = new System.Drawing.Point(59, 174);
            this.label6.Name = "label6";
            this.label6.Size = new System.Drawing.Size(72, 13);
            this.label6.TabIndex = 16;
            this.label6.Text = "Output String:";
    

    Form1.Designer.cs Should look like this so you can call it on another class:

            label8 = new System.Windows.Forms.Label();
    
                label8.AutoSize = true;
            label8.Location = new System.Drawing.Point(219, 26);
            label8.Name = "label8";
            label8.Size = new System.Drawing.Size(35, 13);
            label8.TabIndex = 25;
            label8.Text = "label8";  
    
             // 
            // Form1
            // 
            this.Controls.Add(label8);
    

    some "this." text except the part on "this.Controls.Add" in label8 at Form1.Designer.cs

    And you should call it from the another class like this:

    WindowsFormsApplication999.Form1.label8.Text = "your text here."; //This should modify label8.Text.
    

    edit:

    You should also modify this in Form1.Designer.cs

            private System.Windows.Forms.Label label8;
    

    into this:

            public static System.Windows.Forms.Label label8;
    

提交回复
热议问题