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.
lapuccaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Expert1701Commented:
Here is a complete example:

  using System;
  using System.Windows.Forms;

  public class TestForm : Form
  {
    static void Main()
    {
      Application.EnableVisualStyles();
      Application.SetCompatibleTextRenderingDefault(false);
      Application.Run(new TestForm());
    }

    System.Windows.Forms.SplitContainer splitContainer1;
    System.Windows.Forms.TreeView treeView;
    System.Windows.Forms.ListView listView;
    System.Windows.Forms.ColumnHeader columnHeader1;

    public TestForm()
    {
      InitializeComponent();
    }

    private void InitializeComponent()
    {
      System.Windows.Forms.TreeNode treeNode1 = new System.Windows.Forms.TreeNode("Node16");
      System.Windows.Forms.TreeNode treeNode2 = new System.Windows.Forms.TreeNode("Node17");
      System.Windows.Forms.TreeNode treeNode3 = new System.Windows.Forms.TreeNode("Node18");
      System.Windows.Forms.TreeNode treeNode4 = new System.Windows.Forms.TreeNode("Node19");
      System.Windows.Forms.TreeNode treeNode5 = new System.Windows.Forms.TreeNode("Node8", new System.Windows.Forms.TreeNode[] {
      treeNode1,
      treeNode2,
      treeNode3,
      treeNode4});
      System.Windows.Forms.TreeNode treeNode6 = new System.Windows.Forms.TreeNode("Node20");
      System.Windows.Forms.TreeNode treeNode7 = new System.Windows.Forms.TreeNode("Node21");
      System.Windows.Forms.TreeNode treeNode8 = new System.Windows.Forms.TreeNode("Node22");
      System.Windows.Forms.TreeNode treeNode9 = new System.Windows.Forms.TreeNode("Node23");
      System.Windows.Forms.TreeNode treeNode10 = new System.Windows.Forms.TreeNode("Node9", new System.Windows.Forms.TreeNode[] {
      treeNode6,
      treeNode7,
      treeNode8,
      treeNode9});
      System.Windows.Forms.TreeNode treeNode11 = new System.Windows.Forms.TreeNode("Node10");
      System.Windows.Forms.TreeNode treeNode12 = new System.Windows.Forms.TreeNode("Node11");
      System.Windows.Forms.TreeNode treeNode13 = new System.Windows.Forms.TreeNode("Node12");
      System.Windows.Forms.TreeNode treeNode14 = new System.Windows.Forms.TreeNode("Node13");
      System.Windows.Forms.TreeNode treeNode15 = new System.Windows.Forms.TreeNode("Node14");
      System.Windows.Forms.TreeNode treeNode16 = new System.Windows.Forms.TreeNode("Node15");
      System.Windows.Forms.TreeNode treeNode17 = new System.Windows.Forms.TreeNode("Node0", new System.Windows.Forms.TreeNode[] {
      treeNode5,
      treeNode10,
      treeNode11,
      treeNode12,
      treeNode13,
      treeNode14,
      treeNode15,
      treeNode16});
      System.Windows.Forms.TreeNode treeNode18 = new System.Windows.Forms.TreeNode("Node1");
      System.Windows.Forms.TreeNode treeNode19 = new System.Windows.Forms.TreeNode("Node2");
      System.Windows.Forms.TreeNode treeNode20 = new System.Windows.Forms.TreeNode("Node3");
      System.Windows.Forms.TreeNode treeNode21 = new System.Windows.Forms.TreeNode("Node4");
      System.Windows.Forms.TreeNode treeNode22 = new System.Windows.Forms.TreeNode("Node5");
      System.Windows.Forms.TreeNode treeNode23 = new System.Windows.Forms.TreeNode("Node6");
      System.Windows.Forms.TreeNode treeNode24 = new System.Windows.Forms.TreeNode("Node7");
      this.splitContainer1 = new System.Windows.Forms.SplitContainer();
      this.treeView = new System.Windows.Forms.TreeView();
      this.listView = new System.Windows.Forms.ListView();
      this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
      this.splitContainer1.Panel1.SuspendLayout();
      this.splitContainer1.Panel2.SuspendLayout();
      this.splitContainer1.SuspendLayout();
      this.SuspendLayout();
      //
      // splitContainer1
      //
      this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
      this.splitContainer1.Location = new System.Drawing.Point(0, 0);
      this.splitContainer1.Name = "splitContainer1";
      //
      // splitContainer1.Panel1
      //
      this.splitContainer1.Panel1.Controls.Add(this.treeView);
      //
      // splitContainer1.Panel2
      //
      this.splitContainer1.Panel2.Controls.Add(this.listView);
      this.splitContainer1.Size = new System.Drawing.Size(496, 364);
      this.splitContainer1.SplitterDistance = 165;
      this.splitContainer1.TabIndex = 0;
      //
      // treeView
      //
      this.treeView.Dock = System.Windows.Forms.DockStyle.Fill;
      this.treeView.HideSelection = false;
      this.treeView.Location = new System.Drawing.Point(0, 0);
      this.treeView.Name = "treeView";
      treeNode1.Name = "Node16";
      treeNode1.Text = "Node16";
      treeNode2.Name = "Node17";
      treeNode2.Text = "Node17";
      treeNode3.Name = "Node18";
      treeNode3.Text = "Node18";
      treeNode4.Name = "Node19";
      treeNode4.Text = "Node19";
      treeNode5.Name = "Node8";
      treeNode5.Text = "Node8";
      treeNode6.Name = "Node20";
      treeNode6.Text = "Node20";
      treeNode7.Name = "Node21";
      treeNode7.Text = "Node21";
      treeNode8.Name = "Node22";
      treeNode8.Text = "Node22";
      treeNode9.Name = "Node23";
      treeNode9.Text = "Node23";
      treeNode10.Name = "Node9";
      treeNode10.Text = "Node9";
      treeNode11.Name = "Node10";
      treeNode11.Text = "Node10";
      treeNode12.Name = "Node11";
      treeNode12.Text = "Node11";
      treeNode13.Name = "Node12";
      treeNode13.Text = "Node12";
      treeNode14.Name = "Node13";
      treeNode14.Text = "Node13";
      treeNode15.Name = "Node14";
      treeNode15.Text = "Node14";
      treeNode16.Name = "Node15";
      treeNode16.Text = "Node15";
      treeNode17.Name = "Node0";
      treeNode17.Text = "Node0";
      treeNode18.Name = "Node1";
      treeNode18.Text = "Node1";
      treeNode19.Name = "Node2";
      treeNode19.Text = "Node2";
      treeNode20.Name = "Node3";
      treeNode20.Text = "Node3";
      treeNode21.Name = "Node4";
      treeNode21.Text = "Node4";
      treeNode22.Name = "Node5";
      treeNode22.Text = "Node5";
      treeNode23.Name = "Node6";
      treeNode23.Text = "Node6";
      treeNode24.Name = "Node7";
      treeNode24.Text = "Node7";
      this.treeView.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
      treeNode17,
      treeNode18,
      treeNode19,
      treeNode20,
      treeNode21,
      treeNode22,
      treeNode23,
      treeNode24});
      this.treeView.Size = new System.Drawing.Size(165, 364);
      this.treeView.TabIndex = 0;
      this.treeView.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView_AfterSelect);
      //
      // listView
      //
      this.listView.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
      this.columnHeader1});
      this.listView.Dock = System.Windows.Forms.DockStyle.Fill;
      this.listView.Location = new System.Drawing.Point(0, 0);
      this.listView.Name = "listView";
      this.listView.Size = new System.Drawing.Size(327, 364);
      this.listView.TabIndex = 0;
      this.listView.UseCompatibleStateImageBehavior = false;
      this.listView.View = System.Windows.Forms.View.Details;
      this.listView.DoubleClick += new System.EventHandler(this.listView_DoubleClick);
      //
      // columnHeader1
      //
      this.columnHeader1.Text = "Name";
      this.columnHeader1.Width = 300;
      //
      // TestForm
      //
      this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
      this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
      this.ClientSize = new System.Drawing.Size(496, 364);
      this.Controls.Add(this.splitContainer1);
      this.Name = "TestForm";
      this.Text = "Test Form";
      this.splitContainer1.Panel1.ResumeLayout(false);
      this.splitContainer1.Panel2.ResumeLayout(false);
      this.splitContainer1.ResumeLayout(false);
      this.ResumeLayout(false);
    }

    private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
    {
      listView.Items.Clear();
      foreach (TreeNode node in e.Node.Nodes)
        listView.Items.Add(node.Text);
    }

    private void listView_DoubleClick(object sender, EventArgs e)
    {
      foreach (TreeNode node in treeView.SelectedNode.Nodes)
        if (node.Text == listView.SelectedItems[0].Text)
        {
          treeView.SelectedNode = node;
          break;
        }

    }
  }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mihai StancescuSoftware Engineer Commented:
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
0
lapuccaAuthor Commented:
Hi Mishu,

             TreeNode aNode = listView.SelectedItems[0].Tag as TreeNode;
seems to always returning null.
Thanks.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Mihai StancescuSoftware Engineer Commented:
Sorry I forgot something...
Add this  line at the after select event:
listView.Items.Add(li);


Regards,
Mishu
0
lapuccaAuthor Commented:
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;
            }


        }
0
Mihai StancescuSoftware Engineer Commented:
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
0
Expert1701Commented:
lapucca, did you try the sample application I posted?
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.