Hi all,
I'm pretty new to programming with vb.net. I'm stuck on a problem:
I need to populate a combobox with items and a value for each item.
for example if it were for countries:
Item - Value
Canada - U
France - E
Egypt - A
and such...
I was able to do such things with ASP, but with .net it doesn't seem the same.
I tried: Combobox1.Items.Add("Canada", "U"), but it didn't work.
Any help would be appreciated.
Thanks...

Populating a Combobox (with values)
Craig Lichtenstein - MSFT
Thanks alot for the reply (and great advice)...
But for a simple guy like me who's new to the whole .net thing, I wouldn't how to do any of what you've mentioned (I am pretty new to this whole concept of classes, and OOP so I really don't know how I would go around most of what you've said.)
I have come this far:
Public Sub FillMenu()
Dim conn = DBConn() 'a function the opens a connection to my database
Dim sqlstr As String = "select * from Form_Table"
Dim rs = DBrs(conn, sqlstr) 'a function that runs the query and defines RS
Do While rs.read
ComboBox1.Items.Add(rs("form_name"))
Loop
conn.close()
End Sub
All I need to do is add values along with the items. The above code works as it fills the combobox and I can display the selected items with the following code:
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
TextBox2.Text = ComboBox1.SelectedItem
End Sub
When a user selects an item from the combobox, I'm trying to get the value displayed in another label or a textbox. But since I can't even get the values in the combobox, I'm completely lost... and helpless...
MagicCity33
Matthew Devine
hi,
i thought as well to use hashtable but it didn't work and give me exception when i tried the next code
Dim hshTbl As New HashtablehshTbl.Add(
"A", "Egypt First")hshTbl.Add(
"u", "canada")hshTbl.Add(
"E", "france") Me.ComboBox1.DataSource = hshTbl 'Me.ComboBox1.ValueMember = hshTbl.Keys 'or"key" as aspx 'Me.ComboBox1.DisplayMember = hshTbl.Values ' or"value" as aspxbut this code worked for me
Public Class Form1
'this form contain just one combobox Dim table As New DataTable
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
table.Columns.Add("key", GetType(String))
table.Columns.Add("value", GetType(String))
addrow("A", "Egypt")
addrow("U", "Canada")
addrow("E", "France")
ComboBox1.DataSource = table
ComboBox1.DisplayMember = "value" ComboBox1.ValueMember = "key" End Sub Private Sub addrow(ByVal key As String, ByVal value As String)
Dim row As DataRow = table.NewRow
row("key") = key
row("value") = value
table.Rows.Add(row)
End Sub
End Class
hope this helps
Macca
IJay
when you add item you just add the display member the best thing is to creat a table and to add records to it while the rs read like for example to replace the previous part of code
addrow("A", "Egypt")
whiththis part
While rs.Read()addrow(rs(
"keycolumnName"), rs("valueColumnName")) End WhileGregfig
Thank you everyone for your help...
shakalama - your code worked, and saved me from hours of frustration.
jmcilhinney - thanks again for the dataadapter advice that also worked.
I have solved my problem with both solutions. Thanks to everyone who took time to help.
I have been writing ASP and web code for ages but this .net thing is totally different.
SonalMS