title: How to delete a treenode then select another to keep focus on the tree?
body: <p>Surely I am overlooking something so obvious and simple here, but I just cannot see it.</p>
  
  <p>I have a treeview, nodes are added to it at runtime and can also be deleted. When I delete a node, there is no longer a selected node in the tree, HideSelection is also set to False but that would make no difference anyway as the selected node is deleted, standard behavior I guess.</p>
  
  <p>Anyway, to reduce the number of times and input required focusing back onto the treeview, I want to automatically reselect a node in the tree after one has been deleted (especially useful if deleting lots of nodes in quick succession as it removes the need to click back onto the treeview).</p>
  
  <p>So lets say I have a tree like this:</p>
  
  <p><img src="https://i.stack.imgur.com/EkNcN.jpg" alt="enter image description here"></p>
  
  <p>This is the behavior pattern I am looking for, using the following scenarios as an example:</p>
  
  <ul>
  <li><code>Item1</code> could be selected, when deleted <code>Root</code> becomes selected.</li>
  <li><code>Item2</code> could be selected, when deleted <code>Item6</code> becomes selected.</li>
  <li><code>Item3</code> could be selected, when deleted <code>Item4</code> becomes selected.</li>
  <li><code>Item4</code> could be selected, when deleted <code>Item5</code> becomes selected.</li>
  <li><code>Item5</code> could be selected, when deleted <code>Item4</code> becomes selected.</li>
  <li><code>Item5</code> could be selected, when deleted if <code>Item4</code> is not present, then <code>Item3</code> should be selected.</li>
  <li><code>Item5</code> could be selected, when deleted if <code>Item3</code> or <code>Item4</code> are not present, select <code>Item2</code>.</li>
  <li><code>Item6</code> could be selected, when deleted <code>Item2</code> becomes selected.</li>
  <li><code>Item7</code> could be selected, when deleted <code>Item6</code> becomes selected.</li>
  </ul>
  
  <p>I keep getting Index out of bounds and other AV errors (in Lazarus) and that is without even getting to the point of checking where in the tree the current selected node is.</p>
  
  <p>As it stands right now, in my Delete event I have this:</p>
  
  <pre><code>procedure TMainForm.actDeleteExecute(Sender: TObject);
  var  
    SelNode: TTreeNode;
  begin
    if TreeView1.Selected &lt;&gt; nil then
    begin
      SelNode := TreeView1.Selected;
  
      TreeView1.Selected.Delete;
      TreeView1.SetFocus;
      //ShowMessage(SelNode.GetPrev.Text);  
      TreeView1.Selected.Index := SelNode.Index; 
    end;
  end;
  </code></pre>
  
  <p>Is this a case of me completely misunderstanding the situation again and making the task needlessly difficult, or is there a lot of work involved to implement such behavior?</p>
  
  <p>Many thanks in advance.</p>