Click here to Skip to main content
15,887,822 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
normally only the last change applied on the label text is displayed?
I want to change a label text to be shown step by step?
Posted

1 solution

Yes, in a way you're right (almost). When you change some control data, you need to invalidate it to put data in effect. Usually you do it via Control.Invalidate, but if this is a property like Text, a side effect of the property assignment (you should understand actually this can be a call to some setter method) is calling <code>Invalidate inside the setter.

Invalidation cause rendering to occur. Assuming you're talking about System.Windows.Forms, you form gets the message WM_PAINT, translated to firing of the Paint event. All the invalidated regions are ORed (in the sense of Set Theory for the point sub-sets of the control's client area) and the resulted region is re-rendered. If the data was changed, the view looks different.

As I understand, you want some king of animation — one step at a time. It is simple, if you want to click on some control for every step (button click or whatever). In this case you change data on every click.

If you need timed steps, you can do it using timer (I don't recommend it) in the same way. Better way is using some thread. Using BackgroundWorker is the easiest for your purpose.

C#
Button myButton = //...
Label myLabel = //...
//...
myButton.Click += (sender, eventArgs) => {
    System.ComponentModel.BackgroundWorker worker =
         new System.ComponentModel.BackgroundWorker();
    worker.DoWork += (workerSender, workerEventArgs) => {
        myLabel.Invoke(
        new System.Action<Label>((label) => {
            myLabel.Text = "Phase 1";
        }), myLabel);
        Thread.Sleep(500);
        new System.Action<Label>((label) => {
            myLabel.Text = "Phase 2";
        }), myLabel);
        Thread.Sleep(500);
        //...
    };
};


It it important to understand, that in the thread other than UI thread UI property cannot be assigned directly. Inter-thread invocation should be used (pay attentions: myLabel.Invoke). UI thread is able to queue the request for the delegate changing the label and actually call the property on UI thread.

In real life, myLabel.Text should be sent in invocation as yet another parameter, whole invocation fragment abstracted out in a separate method, etc.

—SA
 
Share this answer
 
v3
Comments
[no name] 31-Jan-11 19:23pm    
Might explain me what is backgroundworker ?
Sergey Alexandrovich Kryukov 31-Jan-11 19:42pm    
Explained by sample. Basically, it's a thread ready for relatively short tasks which are not supposed to run infinitely but the method running in the thread (in this case, a delegate under DoWork) should exit after some activity. There is also a special event System.ComponentModel.BackgroundWorker.RunWorkerCompleted to process the exit. Please see Microsoft help on the topic; it's clear enough, complete with samples.

--SA
Sergey Alexandrovich Kryukov 31-Jan-11 20:18pm    
Aha, aggressive ignorant down-voter is here... a vote of "1"! ha-ha!
Sandeep Mewara 1-Feb-11 2:19am    
Great answer and explaination!

And who downvoted it! Countered. (Looks like a low rep user)
Sergey Alexandrovich Kryukov 1-Feb-11 2:49am    
Reputation measured. Thank you ;-)
--SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900