Reputation: 13103
I have a C# program using WinForms in a Windows Desktop application. It includes a TabControl, and each of 6 tabs has text boxes and so forth. Some of these controls display values that are obtained by the application in background threads.
On occasion, values will be obtained that are to be displayed on controls on a tab that is not visible at that time. When I test it, these values don't show up when I change back to that tab.
My question is: Is there something that prevents Windows.Forms from updating the value on the controls that are not visible at the time of update? They aren't disabled, this happens whether or not they were previously rendered, I'm not coming up with any other reason they wouldn't show up. Can anyone confirm that or tell me I have to look elsewhere? Or even give me another place to look?
Upvotes: 0
Views: 516
Reputation: 13517
I am going to throw up an answer here because of this statement:
Some of these controls display values that are obtained by the application in background threads.
I am going to go out on a limb here and assume you are trying to set the text box's text by using the background thread they were obtained on. If this is the case, you can not modify anything that deals with the UI on any other thread other than the UI thread.
Here is an example of what you do not want to do:
private void button1_Click(object sender, EventArgs e)
{
new Thread(() =>
{
textBox1.Text = "Don't do this";
}).Start();
}
That can have undefined behavior, or even crash your application. What you should do is this:
private void button1_Click(object sender, EventArgs e)
{
new Thread(() =>
{
Invoke(new Action(() =>
{
textBox1.Text = "Hello!";
}));
}).Start();
}
Controls in WinForms (the main form in this case) have a method named Invoke
that will run the delegate on the controls UI thread.
To answer your question:
Is there something that prevents Windows.Forms from updating the value on the controls that are not visible at the time of update?
No. Even if they were disabled, you can still set the controls properties. It doesn't matter if they are hidden either.
If, for whatever reason, this doesn't fix your problem, please just let me know in the comments and I will remove this answer.
Upvotes: 1