|
-
Oct 9th, 2003, 09:20 AM
#1
Thread Starter
I wonder how many charact
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.
-
Oct 9th, 2003, 09:58 AM
#2
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"
-
Oct 9th, 2003, 04:51 PM
#3
Thread Starter
I wonder how many charact
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:
Imports System.Windows.Forms
Imports System.Drawing
Imports System.Collections
Public Class USState
Private myShortName As String
Private myLongName As String
[color=blue]Private _MetroAreas As New MetroAreaCollection[/color]
Public Sub New(ByVal strlongName As String, ByVal strShortName As String)
MyBase.New()
Me.myShortName = strShortName
Me.myLongName = strLongName
End Sub
Public ReadOnly Property ShortName() As String
Get
Return myShortName
End Get
End Property
Public ReadOnly Property LongName() As String
Get
Return myLongName
End Get
End Property
Public Overrides Function ToString() As String
Return Me.ShortName & " - " & Me.LongName
End Function
End Class
State
--MetroArea
----City
-------City.ZipCode
Last edited by nemaroller; Oct 9th, 2003 at 04:56 PM.
-
Oct 9th, 2003, 05:05 PM
#4
Thread Starter
I wonder how many charact
I tried putting in the MetoArea class, something like this:
VB Code:
Public Overrides Function ToString() As String
return _Cities.Item(0).ZipCode
End Function
And I never get anything back. No errors either.
-
Oct 9th, 2003, 05:34 PM
#5
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:
'classes
Public Class USState
Private myShortName As String
Private myLongName As String
Private _MetroAreas As New MetroAreaCollection
Public ReadOnly Property MetroAreas() As MetroAreaCollection
Get
Return _MetroAreas
End Get
End Property
Public Sub New(ByVal strlongName As String, ByVal strShortName As String)
MyBase.New()
Me.myShortName = strShortName
Me.myLongName = strlongName
End Sub
Public ReadOnly Property ShortName() As String
Get
Return myShortName
End Get
End Property
Public ReadOnly Property LongName() As String
Get
Return myLongName
End Get
End Property
Public Overrides Function ToString() As String
Return Me.ShortName & " - " & Me.LongName
End Function
End Class
Public Class MetroAreaCollection
Inherits CollectionBase
Public Function Add(ByVal item As MetroArea) As Integer
Return MyBase.List.Add(item)
End Function
Default Public Property Item(ByVal index As Integer) As MetroArea
Get
Return CType(MyBase.List.Item(index), MetroArea)
End Get
Set(ByVal Value As MetroArea)
MyBase.List.Item(index) = Value
End Set
End Property
End Class
Public Class MetroArea
Private _Name As String
Private _Cities As New CitiesCollection
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public ReadOnly Property Cities() As CitiesCollection
Get
Return _Cities
End Get
End Property
Public Sub New(ByVal name As String)
_Name = name
End Sub
End Class
Public Class CitiesCollection
Inherits CollectionBase
Public Function Add(ByVal item As City) As Integer
Return MyBase.List.Add(item)
End Function
Default Public Property Item(ByVal index As Integer) As City
Get
Return CType(MyBase.List.Item(index), City)
End Get
Set(ByVal Value As City)
MyBase.List.Item(index) = Value
End Set
End Property
End Class
Public Class City
Private _Name As String
Private _Zip As String
Public Property Name() As String
Get
Return _Name
End Get
Set(ByVal Value As String)
_Name = Value
End Set
End Property
Public Property Zip() As String
Get
Return _Zip
End Get
Set(ByVal Value As String)
_Zip = Value
End Set
End Property
Public Sub New(ByVal name As String, ByVal zip As String)
_Name = name
_Zip = Zip
End Sub
End Class
'syntax
Dim test As New USState("California", "CA")
Dim ma As New MetroArea("Riverside")
ma.Cities.Add(New City("Riverside", "92501"))
ma.Cities.Add(New City("Moreno Valley", "92553"))
test.MetroAreas.Add(ma)
ComboBox1.DataSource = test.MetroAreas(0).Cities
ComboBox1.DisplayMember = "Zip"
That works for me.
-
Oct 9th, 2003, 05:37 PM
#6
Actually you don't have to bind directly to the collection if you put the hierarchy in the DisplayMember as well:
VB Code:
'this also works
ComboBox1.DataSource = test.MetroAreas
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:
Dim test As New USState("California", "CA")
Dim ma As New MetroArea("Riverside")
ma.Cities.Add(New City("Riverside", "92501"))
ma.Cities.Add(New City("Moreno Valley", "92553"))
test.MetroAreas.Add(ma)
ma = New MetroArea("Orange")
ma.Cities.Add(New City("Orange", "92525"))
ma.Cities.Add(New City("Anahiem", "92705"))
test.MetroAreas.Add(ma)
ComboBox2.DataSource = test.MetroAreas
ComboBox2.DisplayMember = "Name"
ComboBox1.DataSource = test.MetroAreas
ComboBox1.DisplayMember = "Cities.Zip"
Last edited by Edneeis; Oct 9th, 2003 at 05:41 PM.
-
Oct 9th, 2003, 07:25 PM
#7
Thread Starter
I wonder how many charact
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.
-
Oct 9th, 2003, 08:21 PM
#8
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.
-
Oct 9th, 2003, 08:37 PM
#9
Thread Starter
I wonder how many charact
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.
-
Oct 9th, 2003, 09:49 PM
#10
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|