Link to home
Start Free TrialLog in
Avatar of lapucca
lapucca

asked on

need sample code on how to get the TreeView control to select and foucs on the node that is the parent node of the child node that I juts double clicked on the listivew control. And the TreeView con

Hi, I'm using vs2005 .net 2.0.  I need some sample code for ListView_MouseDoubleClick event. I have a left pane of a treeview control and a right pane of a Listview control that list 1 level of the children nodes of the TreeView's selected node.  I need some sample code to perform behvior like one sees in the Active Directory Users and Computers snap-in.  

I need sample code on how to get the TreeView control to select and foucs on the node that is the parent node of the child node that I juts double clicked on the listivew control.  And the TreeView control would also expand 1 level.  

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Expert1701
Expert1701

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can try this way:
//You can put in the tag property of the list view item the node reference...
private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
    {
      listView.Items.Clear();
      foreach (TreeNode node in e.Node.Nodes)
         {
                 ListViewItem li =  new ListViewItem(node.Text);
                 li.Tag = (object)node;
         }
    }

    private void listView_DoubleClick(object sender, EventArgs e)
    {
       if (listView.SelectedItems[0]!=null)
       {
              TreeNode aNode = listView.SelectedItems[0].Tag as TreeNode;
              if (aNode != null)
                      tree.SelectedNode = aNode;
       }
    }

Hope this helps.
Regards,
Mishu
Avatar of lapucca
lapucca

ASKER

Hi Mishu,

             TreeNode aNode = listView.SelectedItems[0].Tag as TreeNode;
seems to always returning null.
Thanks.
Sorry I forgot something...
Add this  line at the after select event:
listView.Items.Add(li);


Regards,
Mishu
Avatar of lapucca

ASKER

Hi Mishu,

I've added the code as follow but when I double clicked on the Listview's node, nothing happens.  Thanks.


        private void ppTree_AfterSelect(object sender, TreeViewEventArgs e)
        {

            NodeDetail.Items.Clear();
            foreach (TreeNode node in e.Node.Nodes)
            {
                ListViewItem li = new ListViewItem(node.Text);
                li.Tag = (object)node;
                NodeDetail.Items.Add(li);
            }

............


        private void NodeDetail_MouseDoubleClick(object sender, MouseEventArgs e)
        {

            if (NodeDetail.SelectedItems[0]!=null)
            {
               TreeNode aNode = NodeDetail.SelectedItems[0].Tag as TreeNode;
                  if (aNode != null)
                          ppTree.SelectedNode = aNode;
            }


        }
Replace your current method with this:

private void NodeDetail_MouseDoubleClick(object sender, MouseEventArgs e)
        {

            if (NodeDetail.SelectedItems[0]!=null)
            {
               TreeNode aNode = NodeDetail.SelectedItems[0].Tag as TreeNode;
                  if (aNode != null)
                  {
                          ppTree.SelectedNode = aNode;
                          aNode.EnsureVisible();
                          ppTree.Focus();
                  }
            }

        }

Hope it solves the problem.
Regards,
Mishu
lapucca, did you try the sample application I posted?