Aah... you never mentioned datasets...
Code to explain my messed-up witterings:
public partial class Form2 : Form {
public Form2() {
InitializeComponent();
comboBox1.DropDownStyle = ComboBoxStyle.DropDownList;
comboBox1.Items.Clear();
comboBox1.Items.Add(new User("Frodo", 20));
comboBox1.Items.Add(new User("Samwise", 40));
comboBox1.Items.Add(new User("Medideth", 60));
comboBox1.SelectedIndexChanged += new
EventHandler(comboBox1_SelectedIndexChanged);
}
void comboBox1_SelectedIndexChanged(object sender, EventArgs e) {
MessageBox.Show(((User)comboBox1.SelectedItem).ID. ToString());
}
public class User {
public int ID; // yes, cheaky, but quick for demo
public string Name; // likewise
public override string ToString() {
return Name;
}
public User(string name, int id) {
ID = id;
Name = name;
}
}
}
===================
Anyway, if you are using datasets you should be able to bind directly to the
columns, and just bind the ValueMember to be "ID" and use SelectedValue...?
I can't personally give examples to this as (due to the systems I tend to
work on) I don't tend to use datasets at the UI - but MSDN2 is your friend.
Marc
"RSH" <wa*************@yahoo.com> wrote in message
news:um**************@TK2MSFTNGP11.phx.gbl...
I'm having a little problem conceptualizing this...
Here is what I have so far:
public void FillEmployees(DataSet DS, ComboBox ComboList)
{
if (bAuthenticated == true)
{
foreach (DataRow DR in DS.Tables[0].Rows)
{
String sTemp = DR["ID"].ToString().PadLeft(8, '0');
Employee emp = new Employee(); <--------------- How do I create each
object name so it can be referenced later?
emp.FirstName = DR["FirstName"].ToString();
emp.LastName = DR["LastName"].ToString();
emp.ID = sTemp;
ComboList.Items.Add(emp.ToString());
}
}
}
}
class Employee
{
private String iD;
private String firstName;
private String lastName;
public string ID
{
get { return iD; }
set { iD = value; }
}
public string FirstName
{
get { return firstName; }
set { firstName = value; }
}
public string LastName
{
get { return lastName; }
set { lastName = value; }
}
public override string ToString()
{
return ID;
}
}
}
"Marc Gravell" <mg******@rm.com> wrote in message
news:%2***************@TK2MSFTNGP12.phx.gbl... Sory - I meant ComboBox... I think... But the principle stands...
(time for more coffee)
Marc
"Marc Gravell" <mg******@rm.com> wrote in message
news:e9**************@TK2MSFTNGP15.phx.gbl... ComboList? do you mean ListBox?
Anyway... the easiest option is to:
write a class (if you haven't already) that does:
public class User {
public int ID; // yes, cheaky, but quick for demo
public string Name; // likewise
public override string ToString() {
return Name;
}
}
Now, instead of adding the strings, create User objects and add those;
when displaying the system will use the ToString() as the display text.
When you call SelectedValue, just cast back to a User and grab the ID
(or whatever).
There's probably a dozen other ways, but that works - as long as you
don't allow free text input.
Also - why not pass the User object to the event and let the subscriber
worry about what they want to use?
Marc
"RSH" <wa*************@yahoo.com> wrote in message
news:OY**************@TK2MSFTNGP09.phx.gbl...
I need to use a form element to display a list of users. I would like
to store their ID "behind the scenes" so when the user selects a name
the Event passes their ID.
I can't find anything about how to set a Name/Value pair of a
ComboList. Is this the wrong tool for the job? Or how do I set the two
values?
Thanks so much,
Ron