Hi, can anybody tell me the way to fill a combobox without using a dataset (arraylist for example). I want the combobox to display a special string like a name for example, and i want the value to be a string (not simply the index). Thank.
Thank you for the answer but I wanted something a bit different.
Actually, if you bind the combobox to a dataset. You can fill the "DisplayMember" property and the "ValueMember" so that, by code, you can have the Value (that can be any string) using "SelectedValue" property. Do you know how to do this without a dataset
You could try this simple solution, if you really want to bind to an ArrayList.
Create a new class, like this one: Public Class ListItem Public Name As String Public Value As String
Public Sub New(ByVal Name As String, ByVal Value As String) Me.Name = Name Me.Value = Value End Sub
Public Overrides Function ToString() As String Return Name End Function End Class Then, in your form, write code like this (assuming a combobox named ComboBox1 and a Label named Label1): Private al As New ArrayList
Private Sub Form1_Load( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles MyBase.Load
al.Add(New ListItem("Mike", "Item 1")) al.Add(New ListItem("Mary", "Item 2")) al.Add(New ListItem("Andy", "Item 3")) ComboBox1.DataSource = al End Sub
Private Sub ComboBox1_SelectedIndexChanged( _ ByVal sender As System.Object, _ ByVal e As System.EventArgs) _ Handles ComboBox1.SelectedIndexChanged
Label1.Text = DirectCast(al(ComboBox1.SelectedIndex), ListItem).Value End Sub
There are many other solutions, as well. But if you want to bind to a generic ArrayList, this is the technique I would use.
You could use those two properties here, too, but not if you bind the control to a generic ArrayList. If you bind it to a strongly typed collection, then you could set those properties and get the behavior you want.
Filling a Combo box
i16978
Actually, if you bind the combobox to a dataset.
You can fill the "DisplayMember" property and the "ValueMember" so that, by code, you can have the Value (that can be any string) using "SelectedValue" property.
Do you know how to do this without a dataset
Thanks again
MrYanda0
You could try this simple solution, if you really want to bind to an ArrayList.
Create a new class, like this one:
Public Class ListItem
Public Name As String
Public Value As String
Public Sub New(ByVal Name As String, ByVal Value As String)
Me.Name = Name
Me.Value = Value
End Sub
Public Overrides Function ToString() As String
Return Name
End Function
End Class
Then, in your form, write code like this (assuming a combobox named ComboBox1 and a Label named Label1):
Private al As New ArrayList
Private Sub Form1_Load( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles MyBase.Load
al.Add(New ListItem("Mike", "Item 1"))
al.Add(New ListItem("Mary", "Item 2"))
al.Add(New ListItem("Andy", "Item 3"))
ComboBox1.DataSource = al
End Sub
Private Sub ComboBox1_SelectedIndexChanged( _
ByVal sender As System.Object, _
ByVal e As System.EventArgs) _
Handles ComboBox1.SelectedIndexChanged
Label1.Text = DirectCast(al(ComboBox1.SelectedIndex), ListItem).Value
End Sub
There are many other solutions, as well. But if you want to bind to a generic ArrayList, this is the technique I would use.
GoZags
Selven