467,894 Members | 1,572 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

Name Value pair in Combolist

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
Jan 10 '06 #1
  • viewed: 2298
Share:
7 Replies
KJ
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.

Jan 10 '06 #2
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

Jan 10 '06 #3
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


Jan 10 '06 #4
RSH
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



Jan 10 '06 #5
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

"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



Jan 10 '06 #6
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



Jan 10 '06 #7
RSH

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

Ron
"Marc Gravell" <mg******@rm.com> wrote in message
news:Ov**************@TK2MSFTNGP15.phx.gbl...
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

"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



Jan 10 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Ruaidhri | last post: by
5 posts views Thread by brettr | last post: by
7 posts views Thread by Jim Adamson | last post: by
1 post views Thread by jadamson60 | last post: by
16 posts views Thread by Bush is a Fascist | last post: by
reply views Thread by David Lozzi | last post: by
2 posts views Thread by Kevin Blount | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.