Search code examples
c++mfcclistctrl

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? */
       }
 }

Solution

  • 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.