all groups > vb.net controls > september 2004 >
You're in the

vb.net controls

group:

Retrieve info from combobox while adding objects of any type ??



Re: Retrieve info from combobox while adding objects of any type ?? Larry Serflaten
9/29/2004 8:21:47 AM
vb.net controls:
[quoted text, click to view]


How are you adding objects to the combobox, before running the application?

Re: Retrieve info from combobox while adding objects of any type ?? Jay B. Harlow [MVP - Outlook]
9/29/2004 8:24:40 AM
Mendhak,
As Greg suggests (and his sample shows), unless you are using DataBinding
(you set the ListControl.DisplayMember, ValueMember, & DataSource
properties) a ListControl (ComboBox & ListBox) will use Object.ToString for
the display text.

As you found out Button, overrides Object.ToString to display
"System.Windows.Forms.Button, Text: Button1" when it is called.

Your classes are free to override Object.ToString to return meaningful text.

You can use ComboBox.SelectedItem & ListBox.SelectedItem to return the
actual object that is selected. ListControl.SelectedValue will return the
text (ToString/ValueMember) of the selected item...

Hope this helps
Jay

[quoted text, click to view]

Re: Retrieve info from combobox while adding objects of any type ?? Greg Burns
9/29/2004 8:35:22 AM
If I understand you correctly you can you a custom class like so:

add items like this:
cbo.Items.Add(New ListBoxItem(MyObject, "display string"))

retrieve items like this:
Dim MyObject as Object = CType(cbo.SelectedItem, ListBoxItem).Data

Public Class ListBoxItem

Private listItemData As Object
Private listItemText As String

' This is what is displayed in the ComboBox drop-down
Public Overrides Function ToString() As String
Return listItemText
End Function

Public Sub New(ByVal itemData As Object, ByVal itemText As String)

listItemData = itemData
listItemText = itemText
End Sub

Public ReadOnly Property Data() As Object
Get
Data = listItemData
End Get

End Property

Public ReadOnly Property Text() As String
Get
Text = listItemText
End Get

End Property

End Class

HTH,
Greg

[quoted text, click to view]

Retrieve info from combobox while adding objects of any type ?? Mendhak
9/29/2004 5:58:07 PM
Hi All,

When you add an object to a combobox, and run the application, you'll get
something like this:

System.Windows.Forms.Button, Text: Button1

(for example).

Is it possible to have just the text/value in the object to be displayed in
the combobox, yet at the same time when retrieving the combobox item,
getting that same object back?

In this particular case, an object of ANY type can be added to the combobox.

TIA.

Re: Retrieve info from combobox while adding objects of any type ?? Mendhak The Frog
9/30/2004 1:00:00 PM
Really appreciate your help on all of this.
Greg's example was what I needed.

Thanks,
Mendhak.


[quoted text, click to view]

Re: Retrieve info from combobox while adding objects of any type ?? Mendhak The Frog
10/6/2004 12:51:36 PM
Thanks to everyone who responded. I inherited the ComboBox control, and
used Gregg's class.

Some day when I become rich and famous, I'll pay you a trillion dollar
royalty. :D

Thanks,
Mendhak.


[quoted text, click to view]

AddThis Social Bookmark Button