Click here to Skip to main content
15,867,308 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a combobox with treeviewitems.How to get selected treeviewitem into same combobox text
Posted
Comments
hypermellow 17-Sep-14 9:01am    
Hi, can you post your code please? Thanks
BillWoodruff 17-Sep-14 10:00am    
What have you tried ?

1 solution

Here you go. :) If it helps, please mark it as a solution.

C# Version:
private void Main_Load(object sender, EventArgs e)
{
    {
        TV = new TreeView();
        //Create a new instance of the Treeview.
        TV.Location = new Point(100, 104);
        //Set the location of the treeview.
        TV.Size = new Size(100, 50);
        //Set the size of the treeview.
        TV.Nodes.Add("Add Once");
        //Add a Node to the Treeview.
        TV.Nodes.Add("Add Twice");
        //Add another
        CB = new ComboBox();
        //Create a new instance of the Combobox.
        CB.Location = new Point(100, 160);
        //Set the Combobox location.

        TV.AfterSelect += TV_Clicked;
        //Add the AfterSelect Handler to add the items selected to the Combobox.
        Controls.Add(TV);
        //Now add the controls to the form
        Controls.Add(CB);
        //Same
    }
}
//Declare the Treeview.
private TreeView TV;
// Declare the Combobox - We do this instead of droping controls on the form.
private ComboBox CB;
//Handler is added in Event Load.
private void TV_Clicked(object sender, System.EventArgs e)
{

    CB.Items.Add(TV.SelectedNode.Text);
    //Adds the Selected Node to the Combobox.

}


VB.NET Version:
Private WithEvents TV As TreeView 'Declare the Treeview.
Private WithEvents CB As ComboBox ' Declare the Combobox - We do this instead of droping controls on the form.
Private Sub TV_Clicked(ByVal sender As Object, ByVal e As System.EventArgs)'Handler is added in Event Load.

    CB.Items.Add(TV.SelectedNode.Text) 'Adds the Selected Node to the Combobox.

End Sub

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    TV = New TreeView 'Create a new instance of the Treeview.
    TV.Location = New Point(100, 104) 'Set the location of the treeview.
    TV.Size = New Size(100, 50) 'Set the size of the treeview.
    TV.Nodes.Add("And Once") 'Add a Node to the Treeview.
    TV.Nodes.Add("Add Twice") 'Add another
    CB = New ComboBox 'Create a new instance of the Combobox.
    CB.Location = New Point(100, 160) 'Set the Combobox location.

    AddHandler TV.AfterSelect, AddressOf TV_Clicked 'Add the AfterSelect Handler to add the items selected to the Combobox.
    Controls.Add(TV) 'Now add the controls to the form
    Controls.Add(CB) 'Same
End Sub
 
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