0

I'm trying to set the selected node after cleaning and refilling my treeview. Here's the code I tried:

private TreeNode selectednode;
private void ElementTextChanged(object sender, EventArgs e)//saves changes to the XElements displayed in the textboxes
{
    BusinessLayer.ElementName = (sender as TextBox).Tag.ToString();
    string Type = (sender as TextBox).Name;
    string Value = (sender as TextBox).Text;

    if (TView_.SelectedNode!=null)
    {
       selectednode = TView_.SelectedNode; 
    }
    string NodePath = TView_.SelectedNode.FullPath.Replace("\\", "/");

    Telementchange.Stop();
    Telementchange.Interval = 2000;
    Telementchange.Tick += (object _sender, EventArgs _e) => {
        if (Type=="Value")
        {
            BusinessLayer.ChangeElementValue(NodePath,Value);//nembiztos hogy így kéne ezt meghívni
        }
        else
        {

            BusinessLayer.ChangeElementName(NodePath, Value);
            BusinessLayer.ElementName = Value;
        }            
        FillTree(BusinessLayer.Doc);
        TView_.SelectedNode = selectednode; //nemműködikezaszar!!!!!
        TView_.Select();
        Telementchange.Stop(); 
    };
    Telementchange.Start();   
}

For some season after I set the TView_.SelectedNode property it is null. Thank you for helping!

Peter Szekeli
  • 2,712
  • 3
  • 30
  • 44
Bálint Bozsóki
  • 115
  • 1
  • 3
  • 10

2 Answers2

0

Looking at the code you show you seem to do this:

  • store the currently selected Node in a variable
  • clean and refill the TreeView
  • select the stored Node

This is bound to fail as at the moment after the filling, the stored Node is no longer part of the TreeView's node collection unless you have added it again in the fill routine..

I don't think you do that.

If you want to re-select some node you will need to identify it in the new collection of nodes. If the Text is good enough for that do a recursive TreeView search like the one in L.B's answer here in this post (Not the accepted answer, though!)

Community
  • 1
  • 1
TaW
  • 53,122
  • 8
  • 69
  • 111
0

I couldn't solve my problem by setting the SelectedNode property so i made a workaround.

 private void RefreshTreeView()
        {

            FillTree(BusinessLayer.Doc);
            TView_.SelectedNode = _selectednode;
            ExpandToPath(TView_.TopNode, _selectedPath);                    
        }

        void ExpandToPath(TreeNode relativeRoot, string path)
        {
            char delimiter = '\\';
            List<string> elements = path.Split(delimiter).ToList();
            elements.RemoveAt(0);
            relativeRoot.Expand();
            if (elements.Count == 0)
            {
                TView_.SelectedNode = relativeRoot;
                return;
            }
            foreach (TreeNode node in relativeRoot.Nodes)
            {
                if (node.Text == elements[0])
                {
                    ExpandToPath(node, string.Join(delimiter.ToString(),elements));
                }
            }
        }
Bálint Bozsóki
  • 115
  • 1
  • 3
  • 10