|
-
Nov 29th, 2006, 03:24 PM
#1
Thread Starter
Hyperactive Member
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:
Dim myTreeNode As New TreeNode
For Each myTreeNode In myTreeViewMenu.Nodes
cboSection.Items.Add(myTreeNode)
Next
Last edited by gtilles; Nov 29th, 2006 at 06:10 PM.
Truly, you have a dizzying intellect.
-
Nov 29th, 2006, 03:52 PM
#2
Re: Loading objects in a Combobox
VB Code:
Dim myTreeNode As New TreeNode
For Each myTreeNode In myTreeViewMenu.Nodes
cboSection.Items.Add(myTreeNode.text)
Next
Should be as simple as that.
My usual boring signature: Nothing
 
-
Nov 29th, 2006, 04:19 PM
#3
Thread Starter
Hyperactive Member
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.
-
Nov 29th, 2006, 06:12 PM
#4
Thread Starter
Hyperactive Member
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.
-
Nov 29th, 2006, 06:24 PM
#5
Re: Loading objects in a Combobox--RESOLVED-
Use data-binding:
VB Code:
Me.ComboBox1.DisplayMember = "Text"
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:
Dim myTreeNode As [U]New[/U] TreeNode
For Each myTreeNode In myTreeViewMenu.Nodes
cboSection.Items.Add(myTreeNode)
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:
Dim myTreeNode As TreeNode
For Each myTreeNode In myTreeViewMenu.Nodes
cboSection.Items.Add(myTreeNode)
Next
or preferably this:
VB Code:
For Each myTreeNode As TreeNode In myTreeViewMenu.Nodes
cboSection.Items.Add(myTreeNode)
Next
-
Nov 29th, 2006, 06:40 PM
#6
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
 
-
Nov 29th, 2006, 07:17 PM
#7
Thread Starter
Hyperactive Member
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.
-
Nov 29th, 2006, 07:24 PM
#8
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.
-
Nov 29th, 2006, 07:38 PM
#9
Thread Starter
Hyperactive Member
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.
-
Nov 29th, 2006, 07:49 PM
#10
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.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|