In my windows form i have a label and a button. Initially that label will be having some text. On click even of the button i will invoice a function written in different class, in that function i want to change the text of that label.
My Code is
In Form1.cs
1. I have changed modifiers of label to public
private void button1_Click(object sender, EventArgs e)
{
//Call class1's static Method
//Call class1's static Method
Class1.Test();
}
In Class1.cs
In Class1.cs
public static void Test()
{
//Creating the object of Form1
//Creating the object of Form1
Form1 abc = new Form1();
//changing the label text
//changing the label text
abc.lblTest.Text = "Text Changed";
}
By doing like this i am not able to change the text. What is the problem
By doing like this i am not able to change the text. What is the problem
Soft CornerPosted Mar 10, 2011, 1:46 AM
you can do by this also,
private void button1_Click(object sender, EventArgs e)
{
//Call class1's Method
Class1.Test(label1); //your form label id
}
In Class1.cs
public void Test(Label lbl)
{
//changing the label text
lbl.Text = "Text Changed";
}