Name Value pair in Combolist

  • Thread starter Thread starter RSH
  • Start date Start date
R

RSH

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
 
I'm not sure if this is built-in to the existing control or note, but I
do know that you can inherit from System.Windows.Forms.ComboBox, and
add your own internal list to store values.
 
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
 
Sory - I meant ComboBox... I think... But the principle stands...

(time for more coffee)

Marc
 
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;

}

}

}
 
And I meant SelectedItem... <cringes at own ineptitude this evening>

You may also be able to use SelectedValue with binding to the object type,
but that might be more work.

Marc
 
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 said:
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 said:
Sory - I meant ComboBox... I think... But the principle stands...

(time for more coffee)

Marc
 
That definitely worked...now I just need to try and understand what the hell
you built! :-)


Thanks for your help!!!

Ron
 
Back
Top