Menu

#11 Problems Deleting Child Nodes

v2.X
pending
None
1
2018-04-30
2017-07-31
No

I have searched and searchedm but cannot find an answer to my problem. I have command button the opn the Click event should delete current select row. It worls for root level node, but not on a selected child.

Why ?

My delete function is at the bottom of the code below:

    public class  Node
    {
        public string Name { get;  set; }
        public string OffSet { get;  set; }
        public string Description { get;  set; }
        public string Size { get;  set; }
        public bool Base { get; set; }
        public List<Node> Children { get;  set; }

        // constructor
        public Node(string name, string offset, string size, string description, bool blnbase=false)
        {
            this.Name = name;
            this.OffSet = offset;
            this.Size = size;
            this.Description = description;
            this.Children = new List<Node>();
            this.Base = blnbase;
        }
    }
    public List<Node> RegsData;

    private void FillTree()
    {
        // set the delegate that the tree uses to know if a node is expandable
        this.otlvRegs.CanExpandGetter = x => (x as Node).Children.Count > 0;
        // set the delegate that the tree uses to know the children of a node
        this.otlvRegs.ChildrenGetter = x => (x as Node).Children;
        this.otlvRegs.SelectionChanged += delegate (object sender, EventArgs args) {  SelectionChanged(sender, args); };

        // create the tree columns and set the delegates to print the desired object proerty
        var nameCol = new BrightIdeasSoftware.OLVColumn("Name", "Name");
        nameCol.Width = 160;
        nameCol.AspectGetter = x => (x as Node).Name;
        //nameCol.ImageIndex = 0;

        var offsetCol = new BrightIdeasSoftware.OLVColumn("OffSet", "OffSet");
        offsetCol.AspectGetter = x => (x as Node).OffSet;

        var sizeCol = new BrightIdeasSoftware.OLVColumn("Size", "Size");
        sizeCol.AspectGetter = x => (x as Node).Size;

        var descriptionCol = new BrightIdeasSoftware.OLVColumn("Description", "Description");
        descriptionCol.FillsFreeSpace = true;
        descriptionCol.AspectGetter = x => (x as Node).Description;

        // add the columns to the tree
        this.otlvRegs.Columns.Add(nameCol);
        this.otlvRegs.Columns.Add(offsetCol);
        this.otlvRegs.Columns.Add(sizeCol);
        this.otlvRegs.Columns.Add(descriptionCol);
        nameCol.ImageGetter = delegate (object row) {
            if (((Node)row).Base)
                return 0;
            else
                return 1;
        };
        // set the tree roots
        this.otlvRegs.Roots = RegsData;

    }

    private void btnDelete_Click(object sender, EventArgs e)
    {
        object c = otlvRegs.SelectedObject;
        object p = otlvRegs.GetParent(c);
        otlvRegs.RemoveObject(c);
        otlvRegs.RefreshObject(p);
    }

Discussion

  • Phillip Piper

    Phillip Piper - 2018-04-30
     
  • Phillip Piper

    Phillip Piper - 2018-04-30

    The code you have works for root level objects (as you've noted), but for childrem nodes, RemoveObject() isn't doing what you think.

    For children nodes, you need to change your model. As your code stands, Node.Children is unchanged so your ChildrenGetter will still return the same collection.

    You'd need to do something like:

    if (p == null) // no parent, so c is a root level object
          otlvRegs.RemoveObject(c);
     else {
        (p as Node).Children.Remove(c);
        otlvRegs.RefreshObject(p);
    }
    

    That's much more complex than I'd like.

    It's complex because root objects are under your direct control (hence RemoveObject works), but children are controlled by what's in your model -- so you need to modify your model to change what's in the tree.

     
  • Phillip Piper

    Phillip Piper - 2018-04-30
    • status: open --> pending
     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.