0

I am using a TreeListView (ObjectListView) and populated it with a number of items from DB:

class Data
{
    public int ID { get; set; }
    public string Name { get; set; }
    public List<Data> Child;

    public Data(int id, string name)
    {
        ID = id;
        Name = name;
        Child = new List<Data>();
    }
}

How can I select an object (node), scroll tree and expand parent nodes to it (if necessary)? I tried this:

var node = data.SelectMany(x => GetChildren(x)).Where(x => x.ID == 100).FirstOrDefault();
if (node != null)
{                   
    this.tlv.Select();
    this.tlv.SelectObject(node, true);
    <???>
}

For the average WinForms TreeView my code is as follows:

treeView1.SelectedNode = findNodes[j];
findNodes[j].EnsureVisible();
WinAPI.SendMessage(treeView1.Handle, WinAPI.WM_HSCROLL, (IntPtr)WinAPI.SB_LEFT, IntPtr.Zero);
egeo
  • 171
  • 1
  • 19
  • try this: http://stackoverflow.com/questions/10034714/c-sharp-winforms-highlight-treenode-when-treeview-doesnt-have-focus – Tomer Klein Feb 03 '15 at 09:46
  • `tlv.SelectObject` should suffice. The TLV compares objects by reference. Is the object `node` selected from `data` existing in the object list of the TLV (with the same reference)? Maybe you populated the TLV data from a different database query. – Rev Feb 03 '15 at 14:55
  • It works when `treeView1.ExpandAll()`. In this case, I would use `SelectObject(node)` and `EnsureModelVisible(node)`. But when the tree is collapsed these methods do not work and `treeListView1.GetParent(node)` return null (How do I get all the parents, then to expand them?) – egeo Feb 04 '15 at 06:50

1 Answers1

3

ObjectListView has a Reveal() method that does exactly this. From the docs:

Unroll all the ancestors of the given model and make sure it is then visible.

So your code should be simply:

this.tlv.Reveal(node, true);
Grammarian
  • 6,774
  • 1
  • 18
  • 32