Click here to Skip to main content
15,885,027 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have Treeview with
Node
-Node1
-Node1Child
-Node2
-Node2Child

and ComboBox with data
Combo1
Combo2

how to make it work if Node1Child Clicked then in ComboBox Show Node1Child and can be changed woth Combo2 value?

What I have tried:

i search everywhere and cannot find solution for C#
Posted
Updated 19-Feb-17 13:31pm
Comments
Graeme_Grant 19-Feb-17 18:28pm    
Winform, Wpf, UWP? What have you tried so far? Share your code.
Member 12230809 19-Feb-17 18:41pm    
winform
 
            // 
            // treeView1
            // 
            this.treeView1.Location = new System.Drawing.Point(121, 24);
            this.treeView1.Name = "treeView1";
            treeNode1.Name = "Node2";
            treeNode1.Text = "Node2";
            treeNode2.Name = "Node1";
            treeNode2.Text = "Node1";
            treeNode3.Name = "Node1";
            treeNode3.Text = "Node1";
            treeNode4.Name = "Node0";
            treeNode4.Text = "List of Node";
            this.treeView1.Nodes.AddRange(new System.Windows.Forms.TreeNode[] {
            treeNode4});
            this.treeView1.Size = new System.Drawing.Size(147, 215);
            this.treeView1.TabIndex = 2;
            this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
            // 
            // comboBox1
            // 
            this.comboBox1.FormattingEnabled = true;
            this.comboBox1.Items.AddRange(new object[] {
            "list_c1",
            "list_c2",
            "list_c3"});
            this.comboBox1.Location = new System.Drawing.Point(5, 123);
            this.comboBox1.Name = "comboBox1";
            this.comboBox1.Size = new System.Drawing.Size(110, 21);
            this.comboBox1.TabIndex = 3;
            this.comboBox1.SelectedIndexChanged += new System.EventHandler(this.comboBox1_SelectedIndexChanged);


im add treeview data from text file
if (data1 == "1000") treeView1.Nodes[0].Nodes[0].Nodes[0].Text = "Banana";
im trying to make treeview node can be changed with comboBox
Graeme_Grant 19-Feb-17 18:58pm    
You have bound to the events, where is the code for the event handlers?
Member 12230809 19-Feb-17 19:05pm    
 
        private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
        {
            var nodes = treeView1.Nodes;
            comboBox1.DisplayMember = "Text";
            comboBox1.DataSource = nodes;
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            var node = comboBox1.SelectedItem as TreeNode;
            if (node == null)
                return;

            treeView1.SelectedNode = node;
        }


that code only get root node. and Combobox only show root node text not any other comboBox value
Graeme_Grant 19-Feb-17 19:41pm    
What do you want the ComboBox to have to select from?

[Edit:] A TreeView has a Hierarchical data structure. So your code is only working with the first layer - ie: Root layer.

1 solution

The AfterSelect() event only fires for new selections and won't fire if already selected. You may want to capture the TreeView's Click event instead.
C#
private void treeView1_Click(object sender, EventArgs e)
 {
   TreeViewHitTestInfo info = treeView1.HitTest(treeView1.PointToClient(Cursor.Position));
   if (info != null)
     MessageBox.Show(info.Node.Text);
 }


Edit:

Here is what I think that you are attempting to do:
C#
private void treeView1_Click(object sender, EventArgs e)
{
    TreeViewHitTestInfo info = treeView1.HitTest(treeView1.PointToClient(Cursor.Position));
    if (info != null)
    {
        comboBox1.DisplayMember = "Text";
        comboBox1.DataSource = info.Node.Nodes;
    }
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    var node = comboBox1.SelectedItem as TreeNode;
    if (node == null)
        return;
    treeView1.SelectedNode = node;
    treeView1.Focus();
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900