473,394 Members | 1,971 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,394 software developers and data experts.

ComboBox Item (Windows Forms)

I'm baffled by this -- is there not a typed object used for ComboBox Items?
Best I can tell, all of the methods for ComboBox that accept an Item are of
type Object. Why in the world is a common/standard .NET control accepting
an Object as a parameter type?

In Web Forms, there is a ListItem object that can be passed in to
add/retrieve objects from a DropDownItems collection. I searched Google
groups, and all the solutions I'm finding are creating their own custom
name/value pair class, and using that when adding items to a ComboBox.
Seriously?

It would even be forgivable for .NET 1.0 or 1.1, but we're several years
later in .NET 2.0, and it's still using objects?

Please, can someone give me a logical reason behind this? I'd really like
to be wrong on this, and am just totally missing something.

Thanks in advance.
Apr 24 '07 #1
4 6635
All of .Net is based on objects. Everything is an object. You can bind your
ComboBox to a list of strings, a list of objects, a dataset, etc. If you
bind it to a list of string, [item] is a string. If you bind it to a list
of objects, [item] is an object of that type. If you bind it to a dataset
or datatable, [item] is a datarow. I'm not sure exactly what your problem
is, but it seems to make a lot of sense to me.

Robin S.
-----------------------------
"Jerad Rose" <no@spam.comwrote in message
news:ef**************@TK2MSFTNGP03.phx.gbl...
I'm baffled by this -- is there not a typed object used for ComboBox
Items? Best I can tell, all of the methods for ComboBox that accept an
Item are of type Object. Why in the world is a common/standard .NET
control accepting an Object as a parameter type?

In Web Forms, there is a ListItem object that can be passed in to
add/retrieve objects from a DropDownItems collection. I searched Google
groups, and all the solutions I'm finding are creating their own custom
name/value pair class, and using that when adding items to a ComboBox.
Seriously?

It would even be forgivable for .NET 1.0 or 1.1, but we're several years
later in .NET 2.0, and it's still using objects?

Please, can someone give me a logical reason behind this? I'd really
like to be wrong on this, and am just totally missing something.

Thanks in advance.

Apr 25 '07 #2
Thanks for the response, Robin.

Yes, I understand that the object is the fundamental class behind every
class. I guess I wasn't clear on my confusion.

To me, when an object is used as a parameter, it leaves too much open for
type mismatches. For example, since the ComboBox allows you to bind to a
list of strings, a dataset, etc., I would think it would be better to have
these explicitly defined as overloaded parameters (i.e. Add(string[] items),
Add(DataSet dataSet), etc.), rather than just the open-ended Object. This
approach seems to regress us back to the days of VB6 and late binding.

But my main point is this. There is currently no way to add an item to a
combo box that has a different Value and Text property, other than creating
my own name/value pair class, and passing that to the Add method. This
seems ridiculous to me. In fact, I'm not even sure I can set the Text of a
ComboBox Item separately from the Value, since Item is an Object, and
therefore there is no ComboBox1.Items[0].Text property.

Do you see my point?

Thanks again.
Jerad

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:I7******************************@comcast.com. ..
All of .Net is based on objects. Everything is an object. You can bind
your ComboBox to a list of strings, a list of objects, a dataset, etc. If
you bind it to a list of string, [item] is a string. If you bind it to a
list of objects, [item] is an object of that type. If you bind it to a
dataset or datatable, [item] is a datarow. I'm not sure exactly what your
problem is, but it seems to make a lot of sense to me.

Robin S.
-----------------------------
"Jerad Rose" <no@spam.comwrote in message
news:ef**************@TK2MSFTNGP03.phx.gbl...
>I'm baffled by this -- is there not a typed object used for ComboBox
Items? Best I can tell, all of the methods for ComboBox that accept an
Item are of type Object. Why in the world is a common/standard .NET
control accepting an Object as a parameter type?

In Web Forms, there is a ListItem object that can be passed in to
add/retrieve objects from a DropDownItems collection. I searched Google
groups, and all the solutions I'm finding are creating their own custom
name/value pair class, and using that when adding items to a ComboBox.
Seriously?

It would even be forgivable for .NET 1.0 or 1.1, but we're several years
later in .NET 2.0, and it's still using objects?

Please, can someone give me a logical reason behind this? I'd really
like to be wrong on this, and am just totally missing something.

Thanks in advance.


Apr 25 '07 #3
If you have a name/value pair in a class, with a ToString method, and you
bind that to the combobox, I believe you will find the ToString result
shows up in the combobox. If you don't put in the ToString method, I think
it just shows the name of the class or something like that.

Here's an example in VB. I can convert this in C# if you want. This one is
databound. If you want one that's not databound, I can give you that one
too. This is from a small project I wrote that shows 5 different ways to
put data in and take data out of a combobox. If you post your e-mail, I'll
send it to you.
Public Class MainForm
Dim CustomerList As List(Of Customer)
Private _Loading as Boolean

'CustomerTable is already loaded from the database.
Private Sub LoadComboBoxes()
_Loading = True
'add a blank entry to the list
CustomerList = New List(Of Customer)
CustomerList.Add(Customer.Create(0, String.Empty))
Dim CbxCount As Integer = 0
For Each dr As DataRow In CustomerTable.Rows
CustomerList.Add(Customer.Create(CType(dr("Custome rID"),
Integer), _
dr("CustomerName").ToString))
Next dr
ListComboBox.DataSource = CustomerList
ListComboBox.DisplayMember = "CustomerName"
ListComboBox.ValueMember = "CustomerID"
_Loading = False
End Sub

Private Sub ListComboBox_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ListComboBox.SelectedIndexChanged
If Not _Loading AndAlso TableComboBox.SelectedIndex >= 0 Then
Dim cust As Customer = CType(ListComboBox.SelectedItem,
Customer)
MessageBox.Show(String.Format("ID = {0}, Customer Name = {1}",
_
cust.CustomerID, cust.CustomerName), "Combo Box",
MessageBoxButtons.OK, _
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1)
End If
End Sub

Private Class Customer

Private _CustomerName As String
Public Property CustomerName() As String
Get
Return _CustomerName
End Get
Set(ByVal value As String)
_CustomerName = value
End Set
End Property

Private _CustomerID As Integer
Public Property CustomerID() As Integer
Get
Return _CustomerID
End Get
Set(ByVal value As Integer)
_CustomerID = value
End Set
End Property

Private Sub New()

End Sub

Public Shared Function Create(ByVal id As Integer, ByVal name As
String) As Customer
Dim cust As New Customer()
cust.CustomerID = id
cust.CustomerName = name
Return cust
End Function

Public Overrides Function ToString() As String
Return Me.CustomerName
End Function
End Class
End Class

Robin S.
-----------------------
"Jerad Rose" <no@spam.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Thanks for the response, Robin.

Yes, I understand that the object is the fundamental class behind every
class. I guess I wasn't clear on my confusion.

To me, when an object is used as a parameter, it leaves too much open for
type mismatches. For example, since the ComboBox allows you to bind to a
list of strings, a dataset, etc., I would think it would be better to
have these explicitly defined as overloaded parameters (i.e. Add(string[]
items), Add(DataSet dataSet), etc.), rather than just the open-ended
Object. This approach seems to regress us back to the days of VB6 and
late binding.

But my main point is this. There is currently no way to add an item to a
combo box that has a different Value and Text property, other than
creating my own name/value pair class, and passing that to the Add
method. This seems ridiculous to me. In fact, I'm not even sure I can
set the Text of a ComboBox Item separately from the Value, since Item is
an Object, and therefore there is no ComboBox1.Items[0].Text property.

Do you see my point?

Thanks again.
Jerad

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:I7******************************@comcast.com. ..
>All of .Net is based on objects. Everything is an object. You can bind
your ComboBox to a list of strings, a list of objects, a dataset, etc.
If you bind it to a list of string, [item] is a string. If you bind it
to a list of objects, [item] is an object of that type. If you bind it
to a dataset or datatable, [item] is a datarow. I'm not sure exactly
what your problem is, but it seems to make a lot of sense to me.

Robin S.
-----------------------------
"Jerad Rose" <no@spam.comwrote in message
news:ef**************@TK2MSFTNGP03.phx.gbl...
>>I'm baffled by this -- is there not a typed object used for ComboBox
Items? Best I can tell, all of the methods for ComboBox that accept an
Item are of type Object. Why in the world is a common/standard .NET
control accepting an Object as a parameter type?

In Web Forms, there is a ListItem object that can be passed in to
add/retrieve objects from a DropDownItems collection. I searched
Google groups, and all the solutions I'm finding are creating their own
custom name/value pair class, and using that when adding items to a
ComboBox. Seriously?

It would even be forgivable for .NET 1.0 or 1.1, but we're several
years later in .NET 2.0, and it's still using objects?

Please, can someone give me a logical reason behind this? I'd really
like to be wrong on this, and am just totally missing something.

Thanks in advance.



Apr 27 '07 #4
Thanks again Robin for your response.

I've been able to use databinding before to populate ComboBoxes. My main
issue is that there is no built-in method for adding simple value/text pairs
to ComboBoxes. For example, in ASP.NET, I can do this:

MyDropDownList.Items.Add(new ListItem("SomeValue", "SomeText"))

(See http://msdn2.microsoft.com/en-us/library/e7s6873c.aspx)

And be done with it. But in order to accomplish this in WindowsForms, I
have to create my own "ListItem" class (or something similar), and pass it
to the ComboBox.Items.Add method (see
http://msdn2.microsoft.com/en-us/lib...tion.add.aspx).
I understand that it is open to accepting various types of objects, which
actually provides more flexibility. But I would think the preferred
approach would be to set up the Add method to accept a base class (like
ListItem) which can be implemented by other classes for this purpose. But
the DropDownList on Web Forms is completely inconsistent with Windows Forms
ComboBox in this respect.

I'd be curious to find out the reasoning behind this.

Anyway, thanks again for your input.

Jerad

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:J7******************************@comcast.com. ..
If you have a name/value pair in a class, with a ToString method, and you
bind that to the combobox, I believe you will find the ToString result
shows up in the combobox. If you don't put in the ToString method, I think
it just shows the name of the class or something like that.

Here's an example in VB. I can convert this in C# if you want. This one is
databound. If you want one that's not databound, I can give you that one
too. This is from a small project I wrote that shows 5 different ways to
put data in and take data out of a combobox. If you post your e-mail, I'll
send it to you.
Public Class MainForm
Dim CustomerList As List(Of Customer)
Private _Loading as Boolean

'CustomerTable is already loaded from the database.
Private Sub LoadComboBoxes()
_Loading = True
'add a blank entry to the list
CustomerList = New List(Of Customer)
CustomerList.Add(Customer.Create(0, String.Empty))
Dim CbxCount As Integer = 0
For Each dr As DataRow In CustomerTable.Rows
CustomerList.Add(Customer.Create(CType(dr("Custome rID"),
Integer), _
dr("CustomerName").ToString))
Next dr
ListComboBox.DataSource = CustomerList
ListComboBox.DisplayMember = "CustomerName"
ListComboBox.ValueMember = "CustomerID"
_Loading = False
End Sub

Private Sub ListComboBox_SelectedIndexChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ListComboBox.SelectedIndexChanged
If Not _Loading AndAlso TableComboBox.SelectedIndex >= 0 Then
Dim cust As Customer = CType(ListComboBox.SelectedItem,
Customer)
MessageBox.Show(String.Format("ID = {0}, Customer Name = {1}",
_
cust.CustomerID, cust.CustomerName), "Combo Box",
MessageBoxButtons.OK, _
MessageBoxIcon.Information,
MessageBoxDefaultButton.Button1)
End If
End Sub

Private Class Customer

Private _CustomerName As String
Public Property CustomerName() As String
Get
Return _CustomerName
End Get
Set(ByVal value As String)
_CustomerName = value
End Set
End Property

Private _CustomerID As Integer
Public Property CustomerID() As Integer
Get
Return _CustomerID
End Get
Set(ByVal value As Integer)
_CustomerID = value
End Set
End Property

Private Sub New()

End Sub

Public Shared Function Create(ByVal id As Integer, ByVal name As
String) As Customer
Dim cust As New Customer()
cust.CustomerID = id
cust.CustomerName = name
Return cust
End Function

Public Overrides Function ToString() As String
Return Me.CustomerName
End Function
End Class
End Class

Robin S.
-----------------------
"Jerad Rose" <no@spam.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
>Thanks for the response, Robin.

Yes, I understand that the object is the fundamental class behind every
class. I guess I wasn't clear on my confusion.

To me, when an object is used as a parameter, it leaves too much open for
type mismatches. For example, since the ComboBox allows you to bind to a
list of strings, a dataset, etc., I would think it would be better to
have these explicitly defined as overloaded parameters (i.e. Add(string[]
items), Add(DataSet dataSet), etc.), rather than just the open-ended
Object. This approach seems to regress us back to the days of VB6 and
late binding.

But my main point is this. There is currently no way to add an item to a
combo box that has a different Value and Text property, other than
creating my own name/value pair class, and passing that to the Add
method. This seems ridiculous to me. In fact, I'm not even sure I can
set the Text of a ComboBox Item separately from the Value, since Item is
an Object, and therefore there is no ComboBox1.Items[0].Text property.

Do you see my point?

Thanks again.
Jerad

"RobinS" <Ro****@NoSpam.yah.nonewrote in message
news:I7******************************@comcast.com ...
>>All of .Net is based on objects. Everything is an object. You can bind
your ComboBox to a list of strings, a list of objects, a dataset, etc.
If you bind it to a list of string, [item] is a string. If you bind it
to a list of objects, [item] is an object of that type. If you bind it
to a dataset or datatable, [item] is a datarow. I'm not sure exactly
what your problem is, but it seems to make a lot of sense to me.

Robin S.
-----------------------------
"Jerad Rose" <no@spam.comwrote in message
news:ef**************@TK2MSFTNGP03.phx.gbl...
I'm baffled by this -- is there not a typed object used for ComboBox
Items? Best I can tell, all of the methods for ComboBox that accept an
Item are of type Object. Why in the world is a common/standard .NET
control accepting an Object as a parameter type?

In Web Forms, there is a ListItem object that can be passed in to
add/retrieve objects from a DropDownItems collection. I searched
Google groups, and all the solutions I'm finding are creating their own
custom name/value pair class, and using that when adding items to a
ComboBox. Seriously?

It would even be forgivable for .NET 1.0 or 1.1, but we're several
years later in .NET 2.0, and it's still using objects?

Please, can someone give me a logical reason behind this? I'd really
like to be wrong on this, and am just totally missing something.

Thanks in advance.



Apr 27 '07 #5

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

Similar topics

3
by: Wayne Wengert | last post by:
I am trying to populate a combobox with a lisy of items. I want the 1st item to be "Make a Selection" and the following items will be names from a table in my DB (See code below). When I run the...
13
by: Mr. B | last post by:
Here's the situation... You've a combobox with Items already added. Say they look like this (or even lines of text): 10-00-232 10-00-256 10-01-006 10-01-213 10-02-200
8
by: Supa Hoopsa | last post by:
I have a databound combobox within a datagrid. I have set the DisplayMember and ValueMember. BUT, how do I capture the Value (SelectedValue) when the user changes the selection?
0
by: Microsoft | last post by:
Hi All, Any suggestions appreciated. I am trying to do a string search through a comboBox list, have created a simple test which is listed below. The problem I am having is, every time a key is...
7
by: charliewest | last post by:
Using .Net CF, i have created a 2 dimension ArrayList, and "binded" this list to a ComboBox control using the "DataSource" property. I have set the DisplaySource and ValueMember properties as well....
0
by: Andrew Baker | last post by:
Have a look at the code below. It's a really simple example where I have added two controls to a form, a textbox and a combo box. Both add owner drawn context menus to the controls, but the...
0
by: Doug | last post by:
This is a repost of an item that I still cannot resolve. I have 3 combo boxes. The first leads to the second to the third. When I have selected a value in the second box, the third box shows...
5
by: ross kerr | last post by:
Hi All, I am extending the combobox to create a control that selects an item based on the text the user is typing into the text area of the control. I have an issue that occurs only when i...
4
by: Joe Schmoe | last post by:
All I want to to be able to take a two-column DataReader (One column with the Item ID number, the other with Item Description text) and load it into a Windows Forms ComboBox (Set to DropDownList...
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
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...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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
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.