By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,557 Members | 1,603 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,557 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.
Nov 21 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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

"Mendhak" <an*******@discussions.com> wrote in message
news:eZ**************@TK2MSFTNGP14.phx.gbl...
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.

Nov 21 '05 #2

P: n/a

"Mendhak" <an*******@discussions.com> wrote

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).

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

LFS
Nov 21 '05 #3

P: n/a
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

"Mendhak" <an*******@discussions.com> wrote in message
news:eZ**************@TK2MSFTNGP14.phx.gbl...
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.

Nov 21 '05 #4

P: n/a
Really appreciate your help on all of this.
Greg's example was what I needed.

Thanks,
Mendhak.
"Mendhak" <an*******@discussions.com> wrote in message
news:eZ**************@TK2MSFTNGP14.phx.gbl...
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.

Nov 21 '05 #5

P: n/a
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.
"Mendhak" <an*******@discussions.com> wrote in message
news:eZ**************@TK2MSFTNGP14.phx.gbl...
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.

Nov 21 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.