Re: Populating a Combo Box with instances of a class - problem by Martin
Martin
Wed Dec 01 21:37:19 CST 2004
Thank you very much for all of your assistance.
I'm doing this now:
Dim shortcutList As New ArrayList
shortcutList.Add(New Shortcut("Label1", "Full Path1"))
shortcutList.Add(New Shortcut("Label2", "Full Path 2"))
shortcutList.Add(New Shortcut("Label3", "Full Path 3"))
With cmbGo
.DataSource = shortcutList
.ValueMember = "getRealLocation"
.DisplayMember = "toString"
End With
However, I also tried just adding strings to a combo box... such as
cmb.Items.Add("testing") and it still appears blank until I select it =(
I'm so befuddled =(
"Robby" <edmund@not.my.email.com> wrote in message
news:%23WaP89B2EHA.3840@tk2msftngp13.phx.gbl...
>
> The String type is a basic .Net datatype and the ComboBox knows how to
> handel these types. Your Shortcut class is not a basic .Net datatype so
> the ComboBox does not know what to do with it except for calling its
> ToString method when it is the selected item. Your code tells the
> ComboBox what ValueMember and DisplayMember to use but you don't give it a
> data source to use them on. Put your objects in a data source assign that
> to the DataSource property. All will work well once that is done.
>
> Robby
>
> "Martin Smith" <smithmb@ufl.edu> wrote in message
> news:uIvrd.50127$fY.2633@bignews3.bellsouth.net...
>> Hi =)
>>
>> I was under the impression that the DataSource property was to specify a
>> data set such as a collection or data table or or arraylist or whatnot...
>> not a specific piece of data from each object in the combo box. I thought
>> if you just added a bunch of strings to a combo box, when you pulled down
>> the combo box to display multiple entries, it would show all the
>> strings... not blank entries that correspond to the strings.
>>
>> If I'm adding instances of a certain class to a combo box, what should
>> the datasource be set as in order to display a specific data member of
>> that instance, such as the data member "Name" of type string?
>>
>> I'm still not catching the hang of this :)
>>
>> Thanks again.
>>
>> "Robby" <edmund@not.my.email.com> wrote in message
>> news:e2YbKbA2EHA.1192@tk2msftngp13.phx.gbl...
>>>
>>> The ValueMember and DisplayMember only work when the DataSource property
>>> is set. The ComboBox does not have its DataSource property set so it
>>> has nothing to call getRealLocation or toString on. When you select a
>>> blank space it defaults to the ToString method to display it as the
>>> selected item.
>>>
>>> Robby
>>>
>>> "Martin Smith" <smithmb@ufl.edu> wrote in message
>>> news:NVsrd.48894$fY.402@bignews3.bellsouth.net...
>>>> Hi! I'm feeling the burn of .NET; Still getting used to it :)
>>>>
>>>> I'm trying to populate a combo box with instances of a class:
>>>> Public Class Shortcut
>>>> Private Name As String
>>>> Private RealLocation As String
>>>>
>>>> Public Sub New()
>>>> Me.Name = "unknown"
>>>> Me.RealLocation = "unknown"
>>>> End Sub
>>>>
>>>> Public Sub New(ByVal Name As String, ByVal Path As String)
>>>> Me.Name = Name
>>>> Me.RealLocation = Path
>>>> End Sub
>>>>
>>>> Public Function getName() As String
>>>> Return (Me.Name)
>>>> End Function
>>>> Public Function getRealLocation() As String
>>>> Return (Me.RealLocation)
>>>> End Function
>>>>
>>>> Public Overrides Function toString() As String
>>>> Return (Me.Name)
>>>> End Function
>>>> End Class
>>>>
>>>> My problem is the following code:
>>>>
>>>> With cmb
>>>> .ValueMember = "getRealLocation"
>>>> .DisplayMember = "toString"
>>>>
>>>> .Items.Add(New Shortcut("Label1", "Full Path1"))
>>>> .Items.Add(New Shortcut("Label2", "Full Path 2"))
>>>> .Items.Add(New Shortcut("Label3", "Full Path 3"))
>>>> End With
>>>>
>>>> When the combo list isn't activated, the combo box shows the results of
>>>> the toString function, for the specific instance that is selected.
>>>> However, when I click the drop-down button, it shows a list of blank
>>>> entries. Selecting a blank entry again shows the results of the
>>>> toString function of the instance I apparently selected.
>>>>
>>>> How can I make each entry have a value whent he combobox drop-down list
>>>> is showing, not just once a specific item in the list is selected. It's
>>>> hard to make choices when there's only blank options =/.
>>>>
>>>> Thanks again!
>>>>
>>>
>>>
>>
>>
>
>