Click here to Skip to main content
15,867,834 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
I have a TreeView with several nodes and if a special node (you will see in code) is deleted, the parent node should be expanded after updating the TreeView.

I still need help :(

What I have tried:

C#
public void Remove(){
    ...
    ...
    else if ((NodeType)n.Tag == NodeType.Attribute) //Here I simply check if it's the "special" parent
    {
       Commands.CommandAttributeRemove cmd = (Commands.CommandAttributeRemove)mAppData.CommandFactory.Create("AttributeRemove");
       cmd.Data = n.Text;
       cmd.ObjectClass = mObjectClass;
       cmd.ObjectTypeName = n.Parent.Parent.Text;
       list.Add(cmd);
       mNodeToExpand = mTreeView.SelectedNode.Parent; //THIS LINE IS IMPORTANT... mNodeToExpand is a member variable which I use in UpdateData()
    }
    ...
    ...
    UpdateData();
}

public void UpdateData()
    {
       … //A lot of not so important stuff happening here (at least not important for what I want, I think)
       ...
        //Update Selected Items (for the case that objects were deleted) and UpdateSelection
        OnSelect();

        //UpdateSelection();
        this.Update();

Now that's interesting stuff:
C#
    if (mNodeToExpand != null)
        {
            mNodeToExpand.Expand();
            mNodeToExpand = null;
        }
}

This is how I tried to achieve what I want, but the node doesn't expand (it still has other children).
In the
C#
Remove()
I also tried
C#
mTreeView.SelectedNode.Parent.Nodes.Add(new Node("Blabla"));
but it doesn't even add a node;
Posted
Updated 26-Jul-18 9:47am
v2

1 solution

I assume before deleting that the tree is operating normally, expanding when it should. How are you deleting it? I use something like:

pnode.Nodes[i].Remove();

where pnode is the parent node.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900