469,602 Members | 1,717 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,602 developers. It's quick & easy.

ComboBox

I'm used to setting the Text property and Value property of a ComboBox. I
do not see this option for C# 2.0. Am I missing something?

Basically, I'm trying to loop thru an ADO.NET reader object and populating
the combobox with the "Text" and "Identity" column for a database.
Therefore, I need the ID so I can update the correct row.

Mar 22 '06 #1
4 4554
ComboBox never had this... not even in 1.1. The
ComboBox.ObjectCollection contains objects, not a specialized
ComboBoxItem that has Text and a Value. I always found this annoying,
since ListView had ListViewItem that had its text and a Tag field in
which you could store a key.

My impression was that you were supposed to solve this problem using
data bindings, but data bindings were a horrid mess in 1.1 (at least I
thought so).

Mar 22 '06 #2
Thanks -- I'm coming from a VB6 background. For some reason I assumed .NET
had it as the Data Binding to a Combobox seemed to have a selection for
setting this... guess it was an object or something.

In the meantime, I solved this by creating a simple class to hold my two
values. I didn't know if this was the normal way to handle this. Seemed
like a lot of work to me for something that should be relatively simple.

"Bruce Wood" <br*******@canada.com> wrote in message
news:11**********************@u72g2000cwu.googlegr oups.com...
ComboBox never had this... not even in 1.1. The
ComboBox.ObjectCollection contains objects, not a specialized
ComboBoxItem that has Text and a Value. I always found this annoying,
since ListView had ListViewItem that had its text and a Tag field in
which you could store a key.

My impression was that you were supposed to solve this problem using
data bindings, but data bindings were a horrid mess in 1.1 (at least I
thought so).

Mar 23 '06 #3
One thing that you can do is create an object that contains a property
returning text that should show in the ComboBox. Then you set the
ComboBox's DisplayMember to the name of that property, and populate the
ComboBox with these objects, rather than with strings.

I tried this a while back and it caused problems when clearing the
ComboBox of items and repopulating it (which I had to do in some
circumstances). I can't recall the precise details, but it didn't work
as cleanly as it could have.

Nonetheless, it's an option, particularly if you don't need to
repopulate the ComboBox.

Mar 23 '06 #4
On Wed, 22 Mar 2006 17:04:47 -0600, "dm1608" <dm****@spam.net> wrote:
I'm used to setting the Text property and Value property of a ComboBox. I
do not see this option for C# 2.0. Am I missing something?

Basically, I'm trying to loop thru an ADO.NET reader object and populating
the combobox with the "Text" and "Identity" column for a database.
Therefore, I need the ID so I can update the correct row.


Bruce is right there is no text and identity property in the .NET ComboBox.

One way around this is to create a class that you load with the reader. In fact
you can load a class that *IS* the object you are working with. All you need to
do is override the ToString() method of the class so it returns the text you
want the user to see. Then you can use the data contained by the class as you
want.

Here's an example:

// the display class can be used in any project needing to use combo boxes.

using System;
using System.Collections.Generic;
using System.Text;

namespace TestWindow
{
class DisplayObject
{
private string _displayText;
private int _id;

public DisplayObject()
{
}

public DisplayObject(int id, string displayText)
{
_displayText = displayText;
_id = id;
}

public string Text
{
get
{
return _displayText;
}
set
{
_displayText = value;
}
}

public int ID
{
get
{
return _id;
}
set
{
_id = value;
}
}

public override string ToString()
{
return _displayText;
}
}
}

// in the dialog containing the ComboBox...

private void TestDialog_Load(object sender, EventArgs e)
{
DisplayObject disp = new DisplayObject(1, "Test1");
comboBox1.Items.Add(disp);
disp = new DisplayObject(2, "Test2");
comboBox1.Items.Add(disp);
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
DisplayObject disp = (DisplayObject)comboBox1.SelectedItem;
int id = disp.ID;
string text = disp.Text;
}

HTH

Otis Mukinfus
http://www.arltex.com
http://www.tomchilders.com
Mar 23 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

13 posts views Thread by Mr. B | last post: by
3 posts views Thread by TT (Tom Tempelaere) | last post: by
2 posts views Thread by Don | last post: by
reply views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.