473,397 Members | 2,116 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,397 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.
Jul 21 '05 #1
6 3046
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.

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

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

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

Jul 21 '05 #6
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.

Jul 21 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

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...
5
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...
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...
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: 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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.