Results 1 to 10 of 10

Thread: Loading objects in a Combobox--RESOLVED-

  1. #1

    Thread Starter
    Hyperactive Member gtilles's Avatar
    Join Date
    Dec 2004
    Location
    Planet Earth
    Posts
    394

    Loading objects in a Combobox--RESOLVED-

    How can I get the combobox to display what is in mytreenode.text
    and not the control type
    Right now each item looks like this TreeNode: myNodeTitle
    I don't care to have the object type just the title.
    If I load in the just the text then I don't have access to the node values which I need.
    If I load in the node I want it to show the text within the combo list by itself.

    VB Code:
    1. Dim myTreeNode As New TreeNode
    2.         For Each myTreeNode In myTreeViewMenu.Nodes
    3.             cboSection.Items.Add(myTreeNode)
    4.         Next
    Last edited by gtilles; Nov 29th, 2006 at 06:10 PM.
    Truly, you have a dizzying intellect.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Loading objects in a Combobox

    VB Code:
    1. Dim myTreeNode As New TreeNode
    2.         For Each myTreeNode In myTreeViewMenu.Nodes
    3.             cboSection.Items.Add(myTreeNode.text)
    4.         Next

    Should be as simple as that.
    My usual boring signature: Nothing

  3. #3

    Thread Starter
    Hyperactive Member gtilles's Avatar
    Join Date
    Dec 2004
    Location
    Planet Earth
    Posts
    394

    Re: Loading objects in a Combobox

    that only loads the text as a string object not the whole object
    Truly, you have a dizzying intellect.

  4. #4

    Thread Starter
    Hyperactive Member gtilles's Avatar
    Join Date
    Dec 2004
    Location
    Planet Earth
    Posts
    394

    Re: Loading objects in a Combobox--RESOLVED-

    Used a listview instead of a combo
    I ended up creating a listviewitem and assigning the treenode to the tag property, then assigning the listviewitem.text as the treenode.text
    added it to my listview and done....
    Truly, you have a dizzying intellect.

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

    Re: Loading objects in a Combobox--RESOLVED-

    Use data-binding:
    VB Code:
    1. Me.ComboBox1.DisplayMember = "Text"
    2. Me.ComboBox1.DataSource = Me.TreeView1.Nodes
    If you were going to use a loop to add individual items note that this is wrong:
    VB Code:
    1. Dim myTreeNode As [U]New[/U] TreeNode
    2.         For Each myTreeNode In myTreeViewMenu.Nodes
    3.             cboSection.Items.Add(myTreeNode)
    4.         Next
    It will work fine but that first line creates a TreeNode object for nothing. The point of the myTreeNode variable is to refer to the nodes in the TreeView, so you don't create a new node. It should be like this:
    VB Code:
    1. Dim myTreeNode As TreeNode
    2.         For Each myTreeNode In myTreeViewMenu.Nodes
    3.             cboSection.Items.Add(myTreeNode)
    4.         Next
    or preferably this:
    VB Code:
    1. For Each myTreeNode As TreeNode In myTreeViewMenu.Nodes
    2.             cboSection.Items.Add(myTreeNode)
    3.         Next
    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

  6. #6
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,106

    Re: Loading objects in a Combobox--RESOLVED-

    Actually, I thought all you wanted was the string. Referencing the original object via the selectedIndex property should work fine in that case.
    My usual boring signature: Nothing

  7. #7

    Thread Starter
    Hyperactive Member gtilles's Avatar
    Join Date
    Dec 2004
    Location
    Planet Earth
    Posts
    394

    Re: Loading objects in a Combobox--RESOLVED-

    Thanks, jmcilhinney
    I've always had a source of confusion on when to use the New keyword.
    Seems I recall that I was getting object is not set to an instance of an object error when I didn't use it, so I've just done it out of habit.
    Will have to see if I can clean up some code.
    Truly, you have a dizzying intellect.

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

    Re: Loading objects in a Combobox--RESOLVED-

    As far as the New keyword is concerned, just ask yourself "do I need to create a new object". If the answer is "yes" then you need to use the New keyword. If the answer is "no" then you don't. Do you need to create a new TreeNode object in this case? No, you don't. You've already got all the TreeNode objects you want in the TreeView.

    Note that this applies to reference type objects only, i.e. if the type is a class. If the type is a structure then the variable IS the object so there's no specific need to invoke a constructor with New.
    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

  9. #9

    Thread Starter
    Hyperactive Member gtilles's Avatar
    Join Date
    Dec 2004
    Location
    Planet Earth
    Posts
    394

    Re: Loading objects in a Combobox--RESOLVED-

    Holy crap that was awesome!
    I can't believe I've been adding object with for loops all this time when I could have just use DataSource binding.
    Thanks, I learned 3 good lessons from this thread today!
    Truly, you have a dizzying intellect.

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

    Re: Loading objects in a Combobox--RESOLVED-

    Data-binding is very useful but it does have some limitations. You have to place your items in an array or collection in order to bind them to a control. Also, you cannot add or remove items later, at least not directly. You can make changes to the data source and those changes will then be reflected in the control. You can't change an array though, so if your DataSource is an array then you can't change the control contents without creating a completely new array and rebinding.

    Basically, if your objects are simple, like strings or numbers, then you may as well just add them directly to the control, although for adding multiple items you should use AddRange rather than Add. If your objects are complex then data-binding is definitely the way to go.
    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

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