I have 2 forms Form1
and Form2
.
In Form1 i have a backgroundWorker
.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
backgroundWorker1.RunWorkerAsync();
Form2 frm2 = new Form2();
frm2.Show();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
int k = 0;bool b=true;
while (b==true)
{
Thread.Sleep(100);
k++;
backgroundWorker1.ReportProgress(0, "data");
if (k >= 100)
b = false;
}
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string str = "";
str+=e.UserState.ToString();
label1.Text += str;
}
}
In Form2
I have one label label2
.
How can i display the same content of label1 in form1 on label2 in form 2 dynamically.
Please Help with an example.
You can have an Action
that you call when you update the label
on Form1
public Action<string> UpdateFormAction {get; set;}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
string str = "";
str+=e.UserState.ToString();
label1.Text += str;
UpdateForm2(label1.Text);
}
private void UpdateForm2(string text)
{
Action<string> handler = UpdateFormAction;
if (handler != null)
handler(text);
}
Then depending on where you instantiate your forms, you can hook up the Action
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.