Results 1 to 3 of 3

Thread: [RESOLVED] Can I output tree view nodes text in listbox?

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    2

    Resolved [RESOLVED] Can I output tree view nodes text in listbox?

    Ok so first of all I'd like to apolagise if this doesn't make sense. I'm rushing as
    I wrote this all out in slightly more detail before, and then my computer crashed.
    As it's prone to do.

    I'm using Visual Basic 2005 and I'm new to programming. So I'm not sure if this is possible but this is what I want to try and do:

    I have a treeview control it has three parent nodes which are also root nodes and a
    few child nodes under each of these.

    I want the user to be able to select a parent node, and then a child node and then click a button. When the user clicks the button I want the text of a selected child node to be output in a list box on the same form.

    The only thing is that I don't want any of the parent nodes to be output in the
    listbox just the child nodes.

    So I was thinking I could store the selected child nodes text in an array when the user clicks the button, and then output this array in the listbox (also when the user clicks the button.)

    If this is possible I would like the array to be store 14 nodes text (the number of child nodes I have) because I don't really want the user to enter the same detail twice.

    I want the user to select a node, then press the button, and then select another node (if they want to) up to the number of child nodes there are (14). But what's just occured to me is that if I do that, every time the user presses the button all the items in the array will be output again (if I use an array.) I only want one of each child node to be output in the list box. I don't want any repeated stuff so does this mean I have to delete stuff in the array and then add new stuff again everytime? In that case I'd probably only need one nodes text to be stored in the array.

    As you can see I'm really confused but is what I want to do possible? Or is it to complicated for a beginnger? I've been trying everything I know, and then some other things I don't, and searching Google (with no relevant results) so I really need some help.

    I could try and use a list box if it's really to much to understand but I thought a treeview control would make it look more organised...

    Also if anyone knows any links to a help guide or tutorial on this sort of thing that would also be very much appreciated.

    Sorry if this thread is too long.

    Thanks in advance for any help.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Can I output tree view nodes text in listbox?

    You should create a collection of TreeNodes and bind that to your ListBox. You can then add and remove nodes from that collection at will and the ListBox will update, IF you've done it properly.
    vb.net Code:
    1. Imports System.ComponentModel
    2.  
    3. Public Class Form1
    4.  
    5.     Private nodes As New BindingList(Of TreeNode)
    6.  
    7.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    8.         'Bind the collection to the ListBox, displaying the Text property of each item.
    9.         Me.ListBox1.DisplayMember = "Text"
    10.         Me.ListBox1.DataSource = Me.nodes
    11.     End Sub
    12.  
    13.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    14.         If Not Me.nodes.Contains(Me.TreeView1.SelectedNode) Then
    15.             'Add the selected node to the collection.
    16.             Me.nodes.Add(Me.TreeView1.SelectedNode)
    17.  
    18.             'Refresh the bound control.
    19.             Me.nodes.ResetBindings()
    20.         End If
    21.     End Sub
    22.  
    23. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    2

    Re: Can I output tree view nodes text in listbox?

    thank you so much! it seems to be working.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width