Results 1 to 10 of 10

Thread: Possible to set a LIstbox.Datasource to a collection?

  1. #1

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704

    Possible to set a LIstbox.Datasource to a collection?

    I'm trying to set the datasource value of a listbox to a strongly-typed collection.

    .DataSource = myVehicleCollection
    .ValueMember = "Item"
    .DisplayMember = "ToString"

    The Item property in my collection returns a single vehicle.

    On load of the form, I keep getting an error stating the ValueMember could not be set.

  2. #2
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    In this case you would either set the DisplayMember to a property of the item (whatever the item type is in the collection) or not at all. If nothing is set then it uses the ToString method by default so there is no need to set it to that. Also the ToString method is a method not a property so I don't think you can set it to that. It has to be a property. So really all you need is:
    .DataSource = myVehicleCollection

    or
    .DataSource = myVehicleCollection
    .DisplayMember="VehicleName"

  3. #3

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Yes... none of that seems to work.


    If you look up help in the .Net Ide for .DisplayMember, you will see a simplified example of using a collection for a datasource. However, the State class does not contain any child collections.

    Let's pretend that class USState has a child collection of metropolitan areas, and each metro area has a collection of cities....how would one set the Listbox.DisplayMember to point the metro area's city' zipcode property to the Listbox.DisplayMember (or override ToString to point to the State's city suburb's zip code property)

    VB Code:
    1. Imports System.Windows.Forms
    2. Imports System.Drawing
    3. Imports System.Collections
    4.  
    5. Public Class USState
    6.  
    7.     Private myShortName As String
    8.     Private myLongName As String
    9.     [color=blue]Private _MetroAreas As New MetroAreaCollection[/color]
    10.  
    11.     Public Sub New(ByVal strlongName As String, ByVal strShortName As String)
    12.         MyBase.New()
    13.         Me.myShortName = strShortName
    14.         Me.myLongName = strLongName
    15.     End Sub
    16.  
    17.     Public ReadOnly Property ShortName() As String
    18.         Get
    19.             Return myShortName
    20.         End Get
    21.     End Property
    22.  
    23.     Public ReadOnly Property LongName() As String
    24.         Get
    25.             Return myLongName
    26.         End Get
    27.     End Property
    28.  
    29.     Public Overrides Function ToString() As String
    30.         Return Me.ShortName & " - " & Me.LongName
    31.     End Function
    32. End Class

    State
    --MetroArea
    ----City
    -------City.ZipCode
    Last edited by nemaroller; Oct 9th, 2003 at 04:56 PM.

  4. #4

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I tried putting in the MetoArea class, something like this:
    VB Code:
    1. Public Overrides Function ToString() As String
    2.   return _Cities.Item(0).ZipCode
    3. End Function

    And I never get anything back. No errors either.

  5. #5
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Two things: 1) you need to bind directly to the collection that contains the item with the property you want to use in the DisplayMember (i.e USState.MetroAreas(0).Cities) 2) the Displaymember is case sensitive. If it can't find the property in question then it defaults to the ToString method.

    Here is an example:
    VB Code:
    1. 'classes
    2. Public Class USState
    3.  
    4.     Private myShortName As String
    5.     Private myLongName As String
    6.     Private _MetroAreas As New MetroAreaCollection
    7.  
    8.     Public ReadOnly Property MetroAreas() As MetroAreaCollection
    9.         Get
    10.             Return _MetroAreas
    11.         End Get
    12.     End Property
    13.  
    14.     Public Sub New(ByVal strlongName As String, ByVal strShortName As String)
    15.         MyBase.New()
    16.         Me.myShortName = strShortName
    17.         Me.myLongName = strlongName
    18.     End Sub
    19.  
    20.     Public ReadOnly Property ShortName() As String
    21.         Get
    22.             Return myShortName
    23.         End Get
    24.     End Property
    25.  
    26.     Public ReadOnly Property LongName() As String
    27.         Get
    28.             Return myLongName
    29.         End Get
    30.     End Property
    31.  
    32.     Public Overrides Function ToString() As String
    33.         Return Me.ShortName & " - " & Me.LongName
    34.     End Function
    35.  
    36. End Class
    37.  
    38. Public Class MetroAreaCollection
    39.     Inherits CollectionBase
    40.  
    41.     Public Function Add(ByVal item As MetroArea) As Integer
    42.         Return MyBase.List.Add(item)
    43.     End Function
    44.  
    45.     Default Public Property Item(ByVal index As Integer) As MetroArea
    46.         Get
    47.             Return CType(MyBase.List.Item(index), MetroArea)
    48.         End Get
    49.         Set(ByVal Value As MetroArea)
    50.             MyBase.List.Item(index) = Value
    51.         End Set
    52.     End Property
    53.  
    54. End Class
    55.  
    56. Public Class MetroArea
    57.  
    58.     Private _Name As String
    59.     Private _Cities As New CitiesCollection
    60.  
    61.     Public Property Name() As String
    62.         Get
    63.             Return _Name
    64.         End Get
    65.         Set(ByVal Value As String)
    66.             _Name = Value
    67.         End Set
    68.     End Property
    69.  
    70.     Public ReadOnly Property Cities() As CitiesCollection
    71.         Get
    72.             Return _Cities
    73.         End Get
    74.     End Property
    75.  
    76.     Public Sub New(ByVal name As String)
    77.         _Name = name
    78.     End Sub
    79.  
    80. End Class
    81.  
    82. Public Class CitiesCollection
    83.     Inherits CollectionBase
    84.  
    85.     Public Function Add(ByVal item As City) As Integer
    86.         Return MyBase.List.Add(item)
    87.     End Function
    88.  
    89.     Default Public Property Item(ByVal index As Integer) As City
    90.         Get
    91.             Return CType(MyBase.List.Item(index), City)
    92.         End Get
    93.         Set(ByVal Value As City)
    94.             MyBase.List.Item(index) = Value
    95.         End Set
    96.     End Property
    97.  
    98. End Class
    99.  
    100. Public Class City
    101.  
    102.     Private _Name As String
    103.     Private _Zip As String
    104.  
    105.     Public Property Name() As String
    106.         Get
    107.             Return _Name
    108.         End Get
    109.         Set(ByVal Value As String)
    110.             _Name = Value
    111.         End Set
    112.     End Property
    113.  
    114.     Public Property Zip() As String
    115.         Get
    116.             Return _Zip
    117.         End Get
    118.         Set(ByVal Value As String)
    119.             _Zip = Value
    120.         End Set
    121.     End Property
    122.  
    123.     Public Sub New(ByVal name As String, ByVal zip As String)
    124.         _Name = name
    125.         _Zip = Zip
    126.     End Sub
    127.  
    128. End Class
    129.  
    130. 'syntax
    131.         Dim test As New USState("California", "CA")
    132.         Dim ma As New MetroArea("Riverside")
    133.         ma.Cities.Add(New City("Riverside", "92501"))
    134.         ma.Cities.Add(New City("Moreno Valley", "92553"))
    135.         test.MetroAreas.Add(ma)
    136.  
    137.         ComboBox1.DataSource = test.MetroAreas(0).Cities
    138.         ComboBox1.DisplayMember = "Zip"

    That works for me.

  6. #6
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Actually you don't have to bind directly to the collection if you put the hierarchy in the DisplayMember as well:
    VB Code:
    1. 'this also works
    2.         ComboBox1.DataSource = test.MetroAreas
    3.         ComboBox1.DisplayMember = "Cities.Zip"

    To expand the example further if you wanted one combo to be a list of metroareas and the other to be the zips of the cities of the selected metroarea then you'd do this:
    VB Code:
    1. Dim test As New USState("California", "CA")
    2.         Dim ma As New MetroArea("Riverside")
    3.         ma.Cities.Add(New City("Riverside", "92501"))
    4.         ma.Cities.Add(New City("Moreno Valley", "92553"))
    5.         test.MetroAreas.Add(ma)
    6.         ma = New MetroArea("Orange")
    7.         ma.Cities.Add(New City("Orange", "92525"))
    8.         ma.Cities.Add(New City("Anahiem", "92705"))
    9.         test.MetroAreas.Add(ma)
    10.  
    11.         ComboBox2.DataSource = test.MetroAreas
    12.         ComboBox2.DisplayMember = "Name"
    13.  
    14.         ComboBox1.DataSource = test.MetroAreas
    15.         ComboBox1.DisplayMember = "Cities.Zip"
    Last edited by Edneeis; Oct 9th, 2003 at 05:41 PM.

  7. #7

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    I noticed you defined Cities as a ReadOnly Property. Is that required ? Anyway, thanks for the input, I'm gonna log in and see if I can do something with this.

  8. #8
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    No it is not required I was just lazy and didn't want to type the set part. I'm not sure but a default Item property may be required. Good luck and let me know if you get stuck.

  9. #9

    Thread Starter
    I wonder how many charact
    Join Date
    Feb 2001
    Location
    Savage, MN, USA
    Posts
    3,704
    Yea... still not getting it to go.

    I understand why your above examples work. Let me tell you about my specific situation. I have a LISTBOX, whose DATASOURCE property is set to a _Vehicle collection.

    So, you see, trying to have a LISTBOX with a display member such as : _Vehicles.Vehicle.Descriptions.Description

    will not work since you have to provide an index, and the index is really a deciding factor here.

  10. #10
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    What is it you are expecting to see in the list? The description of a particular vehicle? How is _Vehicles.Vehicle.Descriptions.Description different than USState.MetroAreas.MetroArea.Cities.City.Zip?

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