ComboBox

  • Thread starter Thread starter dm1608
  • Start date Start date
D

dm1608

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top