user2832424
user2832424

Reputation: 43

Why CListCtrl Update() not necessary in the example below?

In the code below the ListCtrl will not update the item if i do not use "Update()" after the first "If" conditon but will update even if there is no "Update()" method called after the Second "If" conditon. Why is this? I'm just curious to know when Update() is necessary and when it is not!

   class MyDialog()
   {
    public:
         void MyFunction();
    private:
      CListCtrl myListControl;
   }

   void MyDialog::Myfunction()
   {
       bool bCondition;
       for (auto i = 0, i < myListControl.GetItemCount(); ++i)
       {
         auto n  = myListControl.SetItemText(i, 1, "Start");
         if (n)
           myListControl.Update(i);
         /*Update() is required here */

         EvaluateCondition( bConditon);

         if(bConditon)
            myListControl.SetItemText(i, 1, "End");
         /* Why is Update() ***Not*** required here? */
       }
 }

Upvotes: 2

Views: 1171

Answers (2)

David Ching
David Ching

Reputation: 1963

Update causes the change to be put on the screen immediately. If you don't call it, Windows automatically puts the change on the screen when your message loop next runs (after your MyFunction exits). This is why you need to call it to see "Start", before you change it to "End". Windows automatically updates it to "End" when your function exits.

Upvotes: 4

Vlad Feinstein
Vlad Feinstein

Reputation: 11321

How can you answer question Why X is true? if X is false???

When you set some item's text - the list control invalidates the corresponding area; eventually, when it comes to painting - it will redraw the new text.

According to MSDN (CListCtrl::Update):

Forces the list view control to repaint the item specified by nItem.

So you can see the result immediately.

Upvotes: 0

Related Questions