Solved

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

Posted on 2006-11-08
7
233 Views
Last Modified: 2010-04-16
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.
0
Comment
Question by:lapucca
  • 3
  • 2
  • 2
7 Comments
 
LVL 11

Accepted Solution

by:
Expert1701 earned 500 total points
ID: 17904062
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
 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 17907149
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
 

Author Comment

by:lapucca
ID: 17911410
Hi Mishu,

             TreeNode aNode = listView.SelectedItems[0].Tag as TreeNode;
seems to always returning null.
Thanks.
0
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 17912142
Sorry I forgot something...
Add this  line at the after select event:
listView.Items.Add(li);


Regards,
Mishu
0
 

Author Comment

by:lapucca
ID: 17919701
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
 
LVL 11

Expert Comment

by:Mihai Stancescu
ID: 17920163
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
 
LVL 11

Expert Comment

by:Expert1701
ID: 17940376
lapucca, did you try the sample application I posted?
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question