473,395 Members | 1,462 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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

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
5 1724
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mendhak | last post by:
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...
3
by: zfeld | last post by:
How do I add objects to a comboBox? What I need is something similar to what there was in MFC as MyObj obj; ComboBox cb; int index = cb.Add(obj.name); cb.AddItemData(&obj, index); Which...
5
by: Aaron Ackerman | last post by:
I have a bound combobox the appears on a cell within the column of my bound grid when the user clicks on a cell n(In my vb.net WinForm app). I am trying to allow the adding of an item to that bound...
2
by: SteveR | last post by:
I want to return an ArrayList from my web service. I can write this part and everything compiles but I can't get the web application that uses the web service to compile. I call the service...
1
by: funcSter | last post by:
I want to retrieve data from an Excel file like how I would with a database. I understand that I would have to use OLE DB. Somehow I think I cannot get the connection string right, as the bit of...
0
by: marcobx | last post by:
I have a ComboBox and a List of objects to popolate items. I use the DataSource property and not the List collection: List<foolst = new List<foo>; //add elements into the list //......
19
by: active | last post by:
I'm using a ComboBox to display objects of a class I've defined, say CQQ. Works great except somehow I occasionally set an Item to a String object instead of an object of type CQQ. It looks...
3
by: Przemek M. Zawada | last post by:
Dear Group, I'm developing sample window form, using DataGridView control, which is filled with data through BindingSource, which is based on type of object, as follow: public sampleClass {...
9
by: Cralis | last post by:
I have a List of data objects. I need to populate a ComboBox with the 'Description' field from the objects. I can do that with cmbBx.Items.Add( myObject.Description.ToString() ); myObject has 2...
4
by: rang1 | last post by:
Hi, My VB.Net 2005 application is throwing the following exception when I try to retrieve the selected item from a combobox. Conversion from type 'DataRowView' to type 'String' is not valid ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.