Reputation: 277
I want to update text box while doing something in other class. Let me put my code :
namespace TestApp
{
public partial class Form1 : Form
{
CalledClass call = new CalledClass();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
call.Call_UpdateBox();
}
public void UpdateBox()
{
textBox1.Text = "hello";
}
}
}
namespace TestApp
{
class CalledClass
{
public void Call_UpdateBox()
{
Form1 mainform = new Form1();
//do looping for doing some tasks here and update textbox every time
mainform.UpdateBox();
}
}
}
The Call_UpdateBox function in CalledClass is called when the button on main form is clicked, where I have to do some looping and in between update the textbox in the main form. Though textbox gets updated if i see its value in debug mode, but it reamins blank on main form. Please suggest. Thx in advance.
Upvotes: 1
Views: 13425
Reputation: 4733
The simpliest way
namespace TestApp
{
public partial class Form1 : Form
{
CalledClass call = new CalledClass();
public Form1()
{
InitializeComponent();
call.FormH = this;
}
private void button1_Click(object sender, EventArgs e)
{
call.Call_UpdateBox();
}
public void UpdateBox()
{
textBox1.Text = "hello";
}
}
}
namespace TestApp
{
class CalledClass
{
public static Form1 FormH;
public void Call_UpdateBox()
{
//do looping for doing some tasks here and update textbox every time
FormH.UpdateBox();
}
}
}
Upvotes: 2
Reputation: 4094
You can use Threads
but a simpler way, since you are in WindowsForms is to use a BackgroundWorker,
Your Form1 would be some like:
public partial class Form1 : Form
{
BackgroundWorker _bw = new BackgroundWorker();
CalledClass call = new CalledClass();
public Form1()
{
InitializeComponent();
bw.DoWork += bw_DoWork;
bw_ProgressChanged += bw_ProgressChanged;
}
private void button1_Click(object sender, EventArgs e)
{
if (bw.IsBusy != true)
{
bw.RunWorkerAsync();
}
}
private void bw_DoWork(object sender, DoWorkEventArgs e)
{
call.Call_UpdateBox();
}
private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
textBox1.Text = "hello";
// Here you can access some progress property from CalledClass in order to monitor and inform progress
}
}
Upvotes: 1
Reputation: 48985
You are creating a new form instance, and you don't even show it. So you're not calling UpdateBox()
on the good object instance.
Instead, re-use your current instance of your mainForm
. For example:
public void Call_UpdateBox(Form1 targetForm)
{
targetForm.UpdateBox();
}
private void button1_Click(object sender, EventArgs e)
{
call.Call_UpdateBox(this);
}
Upvotes: 2
Reputation: 11025
You're declaring a new instance of Form1
, not referencing the one that already exists. You should:
namespace TestApp
{
public partial class Form1 : Form
{
CalledClass call = new CalledClass();
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
call.Call_UpdateBox(this);
}
public void UpdateBox()
{
textBox1.Text = "hello";
}
}
}
namespace TestApp
{
class CalledClass
{
public void Call_UpdateBox(Form1 Sender)
{
//do looping for doing some tasks here and update textbox every time
sender.UpdateBox();
}
}
}
Upvotes: 5