combo boxes in .net? [easy question, i'm slow :(]

mjz_5

2[H]4U
Joined
May 24, 2001
Messages
3,637
Hi,

I’m new to the .NET scene, and for the life of me, I can't figure this out..

In HTML, when you create a combobox (selection list), you can have two values. 1) the data being displayed to the user, and 2) an identifier (usually the database ID of the product name being displayed in the list) or something that you want to use within the code.

For example:

<SELECT NAME="products">
<OPTION VALUE="1001">Beer
<OPTION VALUE="1002">Candy
<OPTION VALUE="1003">Steak
</SELECT>

what is the C#.net equivalent to this ?

thanks in advance
 
oo, so there isn't a property in combobox that could store data (like a the value property in an HTML combobox).. shoot, not sure if I explained it correctly.. I am not making a website, I want to make an application. just used the HTML as an example of what i am talking about.
 
edit: oops, took the question the wrong way. I'll look this up in a bit.
 
Mycroft999 said:
The answer to your questions, and a few more questions you haven't asked yet can be found on 4 Guys From Rolla

Specifically this article will help you out: Creating A Data Bound Droplist

Very good stuff there. Espcially if you have no access to VS.NET and don't like Web Matrix and have to hand code everything.

checked this out, seems to be more web specific... I need something for applications.

quote:

They have it for VB6:

http://msdn.microsoft.com/library/d...proitemdata.asp (its for both comboboxes and list boxes)

This is what i want to do in .NET, but i can't find info on it.
 
All the confusion (including my first post) was due to asking about having a name value pair in a ComboBox, then referring to the html select object, and asking how to do it in C#.

Everyone was assuming that you wanted to generate the desired HTML using the C# in an ASP.net page. Though it’s not what you wanted, the HTMLSelect class (basic functionality) or a DropDownList class. General webforms stuff.


On the winforms side though, things are a bit more versatile, though a little more code is required. In a combo box you can specify what property of the objects are supposed to be used for the display and you can have any number of other properties or methods. If you don’t choose one, the output of the “ToString()” method is used for the output.

Given a new winforms application, with a ComboBox and a Label added, the following code should demonstrate this…
Code:
		private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
			label1.Text = "--> " + ((NameValue)comboBox1.SelectedItem).Value;
		}

		private void Form1_Load(object sender, System.EventArgs e) {
			comboBox1.Items.Clear();
			label1.Text = "";
			comboBox1.Items.Add(new NameValue("option a", 1));
			comboBox1.Items.Add(new NameValue("option b", 2));
			comboBox1.Items.Add(new NameValue("option c", 3));
			comboBox1.SelectedIndex = 0;
		}
And add the following structure definition somewhere…
Code:
	struct NameValue{
		public string Name;
		public int Value;

		public NameValue(string Name, int Value){
			this.Name = Name;
			this.Value = Value;
		}

		public override string ToString(){
			return Name;
		}
	}


That will show having a combo box where name value pairs are stored. Of course, the NameValue structure can be replaced with any object, and have whatever members you want, though changing it to a class might be advisable.
 
[MS] said:
All the confusion (including my first post) was due to asking about having a name value pair in a ComboBox, then referring to the html select object, and asking how to do it in C#.

Everyone was assuming that you wanted to generate the desired HTML using the C# in an ASP.net page. Though it’s not what you wanted, the HTMLSelect class (basic functionality) or a DropDownList class. General webforms stuff.


On the winforms side though, things are a bit more versatile, though a little more code is required. In a combo box you can specify what property of the objects are supposed to be used for the display and you can have any number of other properties or methods. If you don’t choose one, the output of the “ToString()” method is used for the output.

Given a new winforms application, with a ComboBox and a Label added, the following code should demonstrate this…
Code:
		private void comboBox1_SelectedIndexChanged(object sender, System.EventArgs e) {
			label1.Text = "--> " + ((NameValue)comboBox1.SelectedItem).Value;
		}

		private void Form1_Load(object sender, System.EventArgs e) {
			comboBox1.Items.Clear();
			label1.Text = "";
			comboBox1.Items.Add(new NameValue("option a", 1));
			comboBox1.Items.Add(new NameValue("option b", 2));
			comboBox1.Items.Add(new NameValue("option c", 3));
			comboBox1.SelectedIndex = 0;
		}
And add the following structure definition somewhere…
Code:
	struct NameValue{
		public string Name;
		public int Value;

		public NameValue(string Name, int Value){
			this.Name = Name;
			this.Value = Value;
		}

		public override string ToString(){
			return Name;
		}
	}


That will show having a combo box where name value pairs are stored. Of course, the NameValue structure can be replaced with any object, and have whatever members you want, though changing it to a class might be advisable.


Great, thanks.. I thought it was confusing, but then I was confused myself to how confusing it really was because of your previous post.

[MS] said:
edit: oops, took the question the wrong way. I'll look this up in a bit.

Now I’m :confused: again .. :p

thanks... I'm new to .NET.. only really worked withJava and oldschool c++

thanks again!!!!!!!!!!! I couldn't find anything on this.. tried MSDN, nothing :( Found VB6 stuff though.
 
So, i played around with it.. And everything makes sense to me. You’re storing a structure for each element in the combobox array.

Just one thing i don’t understand, what does “--> “ represent? I know “+” is concatenation. Is “-->” suppose to be displayed in the combo box list?
 
one thing that probably should be clarified. You don't need to come up with your own class. There is one provided already called System.Web.UI.WebControls.ListItem (but those namespaces are usually imported so you can probably get away with just ListItem, ignoring the custom class code altogether. So Richard's code could be rewritten as:
Code:
		private void Form1_Load(object sender, System.EventArgs e) {
			comboBox1.Items.Clear();
			label1.Text = "";
			comboBox1.Items.Add(new ListItem("option a", 1));
			comboBox1.Items.Add(new ListItem("option b", 2));
			comboBox1.Items.Add(new ListItem("option c", 3));
			comboBox1.SelectedIndex = 0;
		}
as for his --> stuff, that was just for visual beautification of the output I believe. Totally superficial.
 
enkafan said:
one thing that probably should be clarified. You don't need to come up with your own class. There is one provided already called System.Web.UI.WebControls.ListItem (but those namespaces are usually imported so you can probably get away with just ListItem, ignoring the custom class code altogether. So Richard's code could be rewritten as:
Code:
		private void Form1_Load(object sender, System.EventArgs e) {
			comboBox1.Items.Clear();
			label1.Text = "";
			comboBox1.Items.Add(new ListItem("option a", 1));
			comboBox1.Items.Add(new ListItem("option b", 2));
			comboBox1.Items.Add(new ListItem("option c", 3));
			comboBox1.SelectedIndex = 0;
		}
as for his --> stuff, that was just for visual beautification of the output I believe. Totally superficial.

so i guess my question was confusing, because once someone else explained it, you guys know what's going on :p..

yeah, i figured that --> was just cosmetic, but it didn't show up for some reason in the combo box, so i was wondering

thanks to everyone for helping!
 
>those namespaces are usually imported so you can probably get away with just ListItem
In a winform app?

>as for his --> stuff, that was just for visual beautification... Totally superficial.
Yup... :)
 
[MS] said:
In a winform app?
doh! saw the html, and just skimmed the article. I should have known what was up when you were posting an entire class just to get that code to work.
 
Back
Top