Having trouble populating listboxes

A

Anil Gupte

I am having some issues with Listboxes

Here is my code:
Dim DSContent As New DataSet
DSContent = L3GContent.GlobalDataSet
URLListBox.BeginUpdate()
For Each dr As DataRow In DSContent.Tables("ContentSites").Rows
URLListBox.Items.Add(dr("URL"))
Next
URLListBox.EndUpdate()

This populates the listbox and displays the URL as items. However, how do I
put in ValueMembers versus DisplayMembers separately? I tried using
Databindings, but I need more control than what it can offer (for example
adding additional items not in the dataset). Any help is appreciated.

Thanx,
 
K

Kerry Moorman

Anil,

You can add objects to the listbox's Items property and the objects'
ToString method will be displayed.

So create a class with appropriate properties and a ToString method.

Kerry Moorman
 
A

Anil Gupte

Sorry I am not sure what you mean by "Cerate a Class". I have to create a
while new class in order to assign values to the items in the listbox? Or
do you mean create an object and then assign properties to it? How does one
do that? Is there any example code out there?

Thanx,
 
K

kimiraikkonen

Sorry I am not sure what you mean by "Cerate a Class".  I have to createa
while new class in order to assign values to the items in the listbox?  Or
do you mean create an object and then assign properties to it?  How doesone
do that?  Is there any example code out there?

Thanx,
--
Anil Guptewww.keeninc.netwww.icinema.com










- Show quoted text -

Anil,
With wild guess, you may need listview control(coloums, more
areas...), because your post asks:

"However, how do I put in ValueMembers versus DisplayMembers
separately?" .. or something was misunderstood.
 
A

Anil Gupte

No, actually I want a listbox. I want to show the name of a website in the
Listbox (Display Member) and have the URL be the ValueMember so I can pass
it to the browser control to navigate to it when the user clicks on that
item. Third column in the dataset is to be ignored. At the end I want to
put in a couple of other items in the listbox that are not in the dataset.

<rant>I can't understand why Visual Studio makes it so hard to accomplish
simple tasks like this. </rant> :)

I tried creating an array of strinngs and then using addrange, but I get the
error:
Overload resolution failed because no accessible 'AddRange' can be called
with these arguments:
'Public Sub AddRange(items() As Object)': Value of type '2-dimensional
array of String' cannot be converted to '1-dimensional array of
System.Object' because the array types have different numbers of dimensions.
'Public Sub AddRange(value As
System.Windows.Forms.ListBox.ObjectCollection)': Value of type
'2-dimensional array of String' cannot be converted to
'System.Windows.Forms.ListBox.ObjectCollection'.


--
Anil Gupte
www.keeninc.net
www.icinema.com

Sorry I am not sure what you mean by "Cerate a Class". I have to create a
while new class in order to assign values to the items in the listbox? Or
do you mean create an object and then assign properties to it? How does
one
do that? Is there any example code out there?

Thanx,
--
Anil Guptewww.keeninc.netwww.icinema.com










- Show quoted text -

Anil,
With wild guess, you may need listview control(coloums, more
areas...), because your post asks:

"However, how do I put in ValueMembers versus DisplayMembers
separately?" .. or something was misunderstood.
 
L

Lloyd Sheen

Anil Gupte said:
No, actually I want a listbox. I want to show the name of a website in
the Listbox (Display Member) and have the URL be the ValueMember so I can
pass it to the browser control to navigate to it when the user clicks on
that item. Third column in the dataset is to be ignored. At the end I
want to put in a couple of other items in the listbox that are not in the
dataset.

<rant>I can't understand why Visual Studio makes it so hard to accomplish
simple tasks like this. </rant> :)

I tried creating an array of strinngs and then using addrange, but I get
the error:
Overload resolution failed because no accessible 'AddRange' can be called
with these arguments:
'Public Sub AddRange(items() As Object)': Value of type '2-dimensional
array of String' cannot be converted to '1-dimensional array of
System.Object' because the array types have different numbers of
dimensions.
'Public Sub AddRange(value As
System.Windows.Forms.ListBox.ObjectCollection)': Value of type
'2-dimensional array of String' cannot be converted to
'System.Windows.Forms.ListBox.ObjectCollection'.


--
Anil Gupte
www.keeninc.net
www.icinema.com



Anil,
With wild guess, you may need listview control(coloums, more
areas...), because your post asks:

"However, how do I put in ValueMembers versus DisplayMembers
separately?" .. or something was misunderstood.

I think the problem is that you are thinking string vs object here. The
listbox "stores" object references as the items rather than strings. It
will then return the object when you click or change the listbox selected
item. It uses the toString of the object as the display member.

Example:

If I add just strings (as you see to be doing) there is only one property
you can use. So you need to create an object with minimum of two
properties. One is the Website name which you will use for display. The
toString property of the new object (type) should return the Website name.
Now when you capture an event (click) you can retrieve the SelectedItem
property which returns an object (your newly created object). From that you
can get whatever info you need (in your example the URL).

While it may seem more difficult in the long run you will find this much
more flexable and useful espesially if you are using databinding (I do it
all the time).

Think of it as displaying a list of objects from which you can select. Now
you have access thru the click or SelectedIndexChanged to the object which
can have many properties.

Hope this helps
Lloyd Sheen
 
S

Steve Gerrard

Anil said:
No, actually I want a listbox. I want to show the name of a website
in the Listbox (Display Member) and have the URL be the ValueMember
so I can pass it to the browser control to navigate to it when the
user clicks on that item. Third column in the dataset is to be
ignored. At the end I want to put in a couple of other items in the
listbox that are not in the dataset.

Is there a reason why you can't add the extra items to the datatable, and then
bind to it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top