title: When to redraw VirtualTreeView after OnNewText event?
body: <p>I use this code to fill VirtualStringTree and allow renaming items:</p>
  
  <pre><code>//---------------------------------------------------------------------------
  // Structure for the tree
  //---------------------------------------------------------------------------
  struct TVSTdata
  {
  UnicodeString Name;
  };
  //---------------------------------------------------------------------------
  // Initialization of the tree
  //---------------------------------------------------------------------------
  __fastcall TForm1::TForm1(TComponent* Owner) : TForm(Owner)
  {
  VirtualStringTree1-&gt;NodeDataSize = sizeof(TVSTdata);
  // Fill all nodes with initial data
  InitializeTree();
  }
  //---------------------------------------------------------------------------
  // Fill all nodes with data and assign FocusedNode
  //---------------------------------------------------------------------------
  void TForm1::InitializeTree()
  {
  TVirtualNode* pNode;
  TVirtualNode* pActiveNode;
  TVSTdata*     pData;
  
  VirtualStringTree1-&gt;BeginUpdate();
  VirtualStringTree1-&gt;Clear();
  
  pNode = VirtualStringTree1-&gt;AddChild(NULL); pData = static_cast&lt;TVSTdata*&gt;(VirtualStringTree1-&gt;GetNodeData(pNode)); pData-&gt;Name = "This is name 1";
  pNode = VirtualStringTree1-&gt;AddChild(NULL); pData = static_cast&lt;TVSTdata*&gt;(VirtualStringTree1-&gt;GetNodeData(pNode)); pData-&gt;Name = "This is name 2";
  pNode = VirtualStringTree1-&gt;AddChild(NULL); pData = static_cast&lt;TVSTdata*&gt;(VirtualStringTree1-&gt;GetNodeData(pNode)); pData-&gt;Name = "This is name 3"; pActiveNode = pNode;
  pNode = VirtualStringTree1-&gt;AddChild(NULL); pData = static_cast&lt;TVSTdata*&gt;(VirtualStringTree1-&gt;GetNodeData(pNode)); pData-&gt;Name = "This is name 4";
  pNode = VirtualStringTree1-&gt;AddChild(NULL); pData = static_cast&lt;TVSTdata*&gt;(VirtualStringTree1-&gt;GetNodeData(pNode)); pData-&gt;Name = "This is name 5";
  
  VirtualStringTree1-&gt;Selected[pActiveNode] = true;
  VirtualStringTree1-&gt;FocusedNode = pActiveNode; // PROBLEM -&gt; if assigned from within OnNewText will still remain NULL and won't be set to pActiveNode!
  VirtualStringTree1-&gt;EndUpdate();
  }
  //---------------------------------------------------------------------------
  // Just display the text
  //---------------------------------------------------------------------------
  void __fastcall TForm1::VirtualStringTree1GetText(TBaseVirtualTree *Sender, PVirtualNode Node, TColumnIndex Column, TVSTTextType TextType, UnicodeString &amp;CellText)
  {
  TVSTdata* pData = static_cast&lt;TVSTdata*&gt;(Sender-&gt;GetNodeData(Node));
  CellText = pData-&gt;Name;
  }
  //---------------------------------------------------------------------------
  // Allow editing
  //---------------------------------------------------------------------------
  void __fastcall TForm1::VirtualStringTree1Editing(TBaseVirtualTree *Sender, PVirtualNode Node, TColumnIndex Column, bool &amp;Allowed)
  {
  Allowed = true;
  }
  //---------------------------------------------------------------------------
  // Now this is where ideally I would reload the tree with new data - after rename
  //---------------------------------------------------------------------------
  void __fastcall TForm1::VirtualStringTree1NewText(TBaseVirtualTree *Sender, PVirtualNode Node, TColumnIndex Column, UnicodeString NewText)
  {
  NewText = "not important for this example as tree is reloaded anyway";
  InitializeTree();  // ERROR is here - after assigning FocusedNode it is still NULL
  //Timer1-&gt;Enabled = true; // If delayed call FocusedNode is correctly assigned and not NULL
  }
  //---------------------------------------------------------------------------
  void __fastcall TForm1::Timer1Timer(TObject *Sender)
  {
  //InitializeTree();
  //Timer1-&gt;Enabled = false;
  }
  //---------------------------------------------------------------------------
  </code></pre>
  
  <p><strong>The problem</strong> - when <code>InitializeTree()</code> is called initially and <code>VirtualStringTree1-&gt;FocusedNode</code> is assigned it is correctly assigned (not NULL).</p>
  
  <p>However, if this <code>InitializeTree()</code> function is called within <code>OnNewText</code> to actually reload the tree from database after rename event - after assigning <code>FocusedNode</code> it is remains NULL. So obviously tree cannot be reloaded and assigned <code>FocusedNode</code> from within the <code>OnNewText</code> event.</p>
  
  <p>I implemented delayed call to reload new tree and reassign <code>FocusedNode</code> - by implementing a quick and dirty timer (could have used PostMessage for delayed function call but this is just a dumb example) - after assigning within a timer it no longer NULL and works as expected.</p>
  
  <p>Can anyone point me what is the optimal way to implement reloading of the tree - like a particular event to use in which it is safe to set new <code>FocusedNode</code> and it won't be reassigned back to NULL? Is delayed function call the only way to achieve this or is there a better event to trap (for example if one occurs after <code>OnNewText</code> if this one doesn't allow setting focused node). Of course this works but I am interested if there is a better way to do this.</p>