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 thing!
I have one form
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button4_Click(object sender, EventArgs e)
{
adi mYadi= new adi();
adi.paso();
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void l8u(string l )
{
label8.Text = l;
}
}
The l8u
method is supposed to change the text in label8
, so it can't be static because label8
isn't static (is public) and I have this other class
public class adi :instrucion
{
private int paso;
private int registroD;
private int registroO;
private int valor;
private int vsin;
public adi()
{
paso = 1;
}
public void setRD(int i){
registroD = i;
}
public void setR0(int i)
{
registroO = i;
}
public void setV(int i)
{
valor = i;
}
public int getRD()
{
return registroD ;
}
public int getR0()
{
return registroO;
}
public int getVf()
{
return vsin;
}
public void paso(){
//in this method I need change the value of label8
}
}
The method paso is the one in charge of changing the value of label8
but I just can't do it! I've tried many different ways for example doing something like
public void paso()
{
Form1.l8u();
}
But that's not possible since Form1
is just the name of the class and l8u is not and static method, also tried setting label8
as public static but visual studio didn't like that and whenever I used a new control in the form VS change the public static for just public.
Hope you can help me!
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 :(";
}
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
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);
}
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;