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

后端 未结 4 680
旧巷少年郎
旧巷少年郎 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:34

    I know this is 2 years ago but couldnt you just do this

    public static void function(label l)
      {
        l.Text = "Changed text"
      }
    

    and then in the Form do

    private void timer_tick(object sender, EventArgs e)
    {
        function(label);
    }
    
    0 讨论(0)
  • 2021-01-28 01:50

    Simply change your label' s Modifier prperty to internal or public and then call your form and change your label text directly..

    i.e.

    Form2 frm = new Form2(); // Form 2 contains label8 and calling in a method (i.e.buttonclick) of form1
    
    if (List<WhoLovesMe>.Count > 0)
    {
       frm.Label8.Text = "Someone Loves Me :)";
    }
    else
    {
       frm.Label8.Text = "Noone Loves Me :(";
    }
    
    0 讨论(0)
  • 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;
    
    0 讨论(0)
  • 2021-01-28 01:53

    Changing the label in that manner is not a good idea and violates some programming paradigms. Generally, the underlying business logic classes are not supposed to directly manipulate the UI.

    The form contains an instance of adi. So, short of passing the form's instance (ie. this) to the adi constructor (or to the paso method), you're kinda sunk.

    Better to use some kind of event that adi can fire when it needs Form1 to change its display.

    http://msdn.microsoft.com/en-us/library/aa645739(v=vs.71).aspx

    0 讨论(0)
提交回复
热议问题