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