DropDownList in C# Application
-
Hi All, I have a form which has a dropdown list on it which is being populated by an array. What I am trying to find out how to do is set a value for a option in the dropdown list so that I can query the database using a Integer and not a text field. Example: :) My Drop Down list has the following Option 1: London Option 2: Liverpool Option 3: Other If my user selects 'Liverpool' I would like to get the value '42' which is the unique ID in the database that is connected to Liverpool. In ASP and VB you where just able to add a value to the field, can this be done in C#? Thanks,
-
Hi All, I have a form which has a dropdown list on it which is being populated by an array. What I am trying to find out how to do is set a value for a option in the dropdown list so that I can query the database using a Integer and not a text field. Example: :) My Drop Down list has the following Option 1: London Option 2: Liverpool Option 3: Other If my user selects 'Liverpool' I would like to get the value '42' which is the unique ID in the database that is connected to Liverpool. In ASP and VB you where just able to add a value to the field, can this be done in C#? Thanks,
If your working in ASP.net, one of the the Add overrides lets you specify a ListItem class, which takes two strings. These map to the text of the option and the value of the option in html. Value can be retrieved from SelectedItem.Value, and the text from SelectedItem.Text. I expect C# for apps is fairly close, but there are definately differences between the two. One difference I expect is the ability to map an object to the string instead of a second string. // Rock
-
If your working in ASP.net, one of the the Add overrides lets you specify a ListItem class, which takes two strings. These map to the text of the option and the value of the option in html. Value can be retrieved from SelectedItem.Value, and the text from SelectedItem.Text. I expect C# for apps is fairly close, but there are definately differences between the two. One difference I expect is the ability to map an object to the string instead of a second string. // Rock
Using ASP I have been able to do it and that is why I expect it to be at least similar in a C# Application. There just doesn't seem to be a possible option for value which is mind boggling if they have decided to scrap that one. Please let me know if you come across anything that explains this, also how do you mean CLaW wrote: One difference I expect is the ability to map an object to the string instead of a second string. ???? Thanks
-
Hi All, I have a form which has a dropdown list on it which is being populated by an array. What I am trying to find out how to do is set a value for a option in the dropdown list so that I can query the database using a Integer and not a text field. Example: :) My Drop Down list has the following Option 1: London Option 2: Liverpool Option 3: Other If my user selects 'Liverpool' I would like to get the value '42' which is the unique ID in the database that is connected to Liverpool. In ASP and VB you where just able to add a value to the field, can this be done in C#? Thanks,
Hi Gavin The ComboBox in .NET receives a generic object. This is pretty powerful, but also is quite a change to the normal VB-style. All that's required is to create a simple (or more complex) class and use that to add to the ComboBox. To get values out, just cast to your object and call your custom props. The ToString() override caught me out, but the ComboBOx uses it to show values. private void Form1_Load(object sender, System.EventArgs e) { comboBox1.Items.Clear(); comboBox1.Items.Add(new cComboLoader("Bill",1)); comboBox1.Items.Add(new cComboLoader("Steve",2)); comboBox1.Items.Add(new cComboLoader("Paul",3)); comboBox1.Items.Add(new cComboLoader("Reinout",4)); } private void button1_Click(object sender, System.EventArgs e) { MessageBox.Show(((cComboLoader)comboBox1.SelectedItem).ToString()); MessageBox.Show(((cComboLoader)comboBox1.SelectedItem).myIndexVal().ToString()); } public class cComboLoader { private string _Txt=""; private int _iVal=0; public cComboLoader(string sTxt, int iVal) { _Txt=sTxt; _iVal=iVal; } public override string ToString() { return _Txt; } public int myIndexVal() { return _iVal; } } Does this help you? Cheers, Simon X-5 452 rules.
-
Hi Gavin The ComboBox in .NET receives a generic object. This is pretty powerful, but also is quite a change to the normal VB-style. All that's required is to create a simple (or more complex) class and use that to add to the ComboBox. To get values out, just cast to your object and call your custom props. The ToString() override caught me out, but the ComboBOx uses it to show values. private void Form1_Load(object sender, System.EventArgs e) { comboBox1.Items.Clear(); comboBox1.Items.Add(new cComboLoader("Bill",1)); comboBox1.Items.Add(new cComboLoader("Steve",2)); comboBox1.Items.Add(new cComboLoader("Paul",3)); comboBox1.Items.Add(new cComboLoader("Reinout",4)); } private void button1_Click(object sender, System.EventArgs e) { MessageBox.Show(((cComboLoader)comboBox1.SelectedItem).ToString()); MessageBox.Show(((cComboLoader)comboBox1.SelectedItem).myIndexVal().ToString()); } public class cComboLoader { private string _Txt=""; private int _iVal=0; public cComboLoader(string sTxt, int iVal) { _Txt=sTxt; _iVal=iVal; } public override string ToString() { return _Txt; } public int myIndexVal() { return _iVal; } } Does this help you? Cheers, Simon X-5 452 rules.
Wow, just a little bit more complicated than VB :)... Well that is exactly what I was looking for but as usual it has arrived about 2 hours after I came up with another solution. I will keep it in my code bank though because it is most probably better than mine :).. What I have done is load a 2 dimensional array with the ID's and strings, then loaded the combobox with the string. When a option is chosen it loops through my Array to find the matching ID. Hey it works ;p Thanks for the help though
-
Wow, just a little bit more complicated than VB :)... Well that is exactly what I was looking for but as usual it has arrived about 2 hours after I came up with another solution. I will keep it in my code bank though because it is most probably better than mine :).. What I have done is load a 2 dimensional array with the ID's and strings, then loaded the combobox with the string. When a option is chosen it loops through my Array to find the matching ID. Hey it works ;p Thanks for the help though
-
Wow, just a little bit more complicated than VB :)... Well that is exactly what I was looking for but as usual it has arrived about 2 hours after I came up with another solution. I will keep it in my code bank though because it is most probably better than mine :).. What I have done is load a 2 dimensional array with the ID's and strings, then loaded the combobox with the string. When a option is chosen it loops through my Array to find the matching ID. Hey it works ;p Thanks for the help though
There is another way, it involves using the DataSource, DisplayMember and ValueMember properties of the combo box. If I weren't late for work I'd describe exactly how to use it, but if you search the docs for DisplayMember there is an example of using it :) James Sonork ID: 100.11138 - Hasaki "My words but a whisper -- your deafness a SHOUT. I may make you feel but I can't make you think." - Thick as a Brick, Jethro Tull 1972