Home All Groups Group Topic Archive Search About

is there a way to do this

Author
14 Apr 2006 1:07 PM
rodchar
hey all,

i'm using a listbox in my win form and was wondering if i can load an item
to my listbox giving it a name/value pair (like dropdownlist) because i
noticed that the available properties shows a Seletected Value and Selected
Item. What would the syntax look like if this is possible?

thanks,
rodchar

Author
14 Apr 2006 2:43 PM
Claes Bergefall
Those properties are used when binding it to a datasource
In your case it's probably easier to build a small class that holds all the
data that you need, add a (overridden) ToString method and add instances of
that class to your listbox. The item collection is not limited to strings,
you can put anything in there. The listbox will call the ToString method of
the object to determine the string to display. Example:

Public Class MyItem
    Public Name As String
    Public Value As Integer
    Public Sub New(name As String, value As Integer)
        Me.Name = name
        Me.Value = value
    End Sub
    Public Overrides Function ToString() As String
        Return Me.Name
    End Function
End Class
....
myListBox.Items.Add(New MyItem(name1, value1))
myListBox.Items.Add(New MyItem(name2, value3))
myListBox.Items.Add(New MyItem(name3, value3))
....
Dim item As MyItem
item = CType(myListBox.SelectedItem, MyItem)

    /claes

Show quoteHide quote
"rodchar" <rodc***@discussions.microsoft.com> wrote in message
news:7A159B5F-DC0E-4995-A6BF-57E095873F27@microsoft.com...
> hey all,
>
> i'm using a listbox in my win form and was wondering if i can load an item
> to my listbox giving it a name/value pair (like dropdownlist) because i
> noticed that the available properties shows a Seletected Value and
> Selected
> Item. What would the syntax look like if this is possible?
>
> thanks,
> rodchar
Author
18 Apr 2006 6:17 PM
Chris Dunaway
And if you bind the list box to a BindingList(Of MyItem) then it will
automatically updated as  you add/removed items from the list.  That
way you can keep your data model separate from the UI.
Author
14 Apr 2006 2:54 PM
Herfried K. Wagner [MVP]
"rodchar" <rodc***@discussions.microsoft.com> schrieb:
> i'm using a listbox in my win form and was wondering if i can load an item
> to my listbox giving it a name/value pair (like dropdownlist) because i
> noticed that the available properties shows a Seletected Value and
> Selected
> Item. What would the syntax look like if this is possible?

Check out the code samples at
<URL:http://dotnet.mvps.org/dotnet/code/controls/#ItemData>.

--
M S   Herfried K. Wagner
M V P  <URL:http://dotnet.mvps.org/>
V B   <URL:http://classicvb.org/petition/>
Author
14 Apr 2006 6:15 PM
rodchar
thank you for the great help.

Show quoteHide quote
"rodchar" wrote:

> hey all,
>
> i'm using a listbox in my win form and was wondering if i can load an item
> to my listbox giving it a name/value pair (like dropdownlist) because i
> noticed that the available properties shows a Seletected Value and Selected
> Item. What would the syntax look like if this is possible?
>
> thanks,
> rodchar