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!
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 theadi
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
Simply change your label' s Modifier prperty to internal or public and then call your form and change your label text directly..
i.e.
I know this is 2 years ago but couldnt you just do this
and then in the Form do
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:
Form1.Designer.cs Should look like this so you can call it on another class:
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:
edit:
You should also modify this in Form1.Designer.cs
into this: