Xantham
Xantham

Reputation: 1899

In C# label.Update() does not work, but textbox.Update() does?

So I am doing a somewhat lengthy progress in another class, and I want to give some progress info to my GUI. I am aware of background worker, and may use it for this if I HAVE to, but this operation is so simple that I feel that background worker is a bit more than I need. Instead, I am using eventhandlers to handle the updates, but the operation is inconsistent.

When a point has been reached in the worker class, it puts up an event telling about its progress. In the GUI class, I have an eventhandler listening for that event. When it finds it, it makes a string about it, and puts that string as the text of a label.

I then call labelname.Update() in that eventhandler, but nothing happens. Here's the really confusing part, I put a textbox there instead, set its text, and then called textboxname.Update(), and it worked. Why would .Update() not work for one control, but not another. Is there a trick to get it to work for a label?

Upvotes: 2

Views: 3412

Answers (2)

kol
kol

Reputation: 28688

(1) Try to call labelname.Invalidate(); before calling labelname.Update();

Calling the Invalidate method does not force a synchronous paint; to force a synchronous paint, call the Update method after calling the Invalidate method. (MSDN)

(2) Another option is calling labelname.Refresh();

Forces the control to invalidate its client area and immediately redraw itself and any child controls. (MSDN)

Upvotes: 1

Dabblernl
Dabblernl

Reputation: 16121

I am turning Roken's comment into an answer, but his remark is the answer. You must implement the background operation in a background worker, period.

See this excellent article about the why and how.

Upvotes: 2

Related Questions