Adding Value and text to a Listbox or a combobox

Hi...

I have a very basic question, but i have suffered for it for more than 3 days.
I need a sample code or a method to add text and value to a combobox and listbox..
Can you help me in that please ?
Thanks
[275 byte] By [Ahmedabugh] at [2007-12-16]
# 1
The answer to both is to use the .items collections.

Exmaple.



ComboBox1.Items.Add("Test string")
ListBox1.Items.Add(
"Test String")

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 2
I believe that to have a value associated with the displayed text you would need to have bound your ComboBox or ListBox to an object that implements the IList interface. You can then set the DisplayMember and ValueMember properties to draw the displayed text and values from that object. To add a text/value pair to the control you would have to add it to the object it was bound to. Look up the help topics for the DataSource, DisplayMember and ValueMember properties of the ComboBox or ListBox for more information.
jmcilhinney at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 3
Oh, i didn't see you wanted to add a value and object ;)

I think what jmcilhinney said would work, but here's another example i use when i want to do that.

Basically, i keep a ArrayList, and use my own functions to add/remove items to the listbox control. As you see in the form load() event, there's AddItemToList() and RemoveItemFromList().

I'm sure if you have more then one listbox, you can easily modify this to accept which listbox/arraylist to use.



Public Class Form1

Private ListCollection As New ArrayList
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddItemToList(
"Number", 123)
AddItemToList(
"Object", New Point(10, 15))
AddItemToList(
"String", "String here")
AddItemToList(
"Another Object", New MonthCalendar)
AddItemToList(
"Another Object", New WebBrowser) 'Same name, this will be skipped.
RemoveItemFromList(
"String") 'Remove item.
RemoveItemFromList(
"Object") 'Remove item.
End Sub
Private Sub AddItemToList(ByVal Name As String, ByVal Item As Object)

'Check to see if it's there already, then add it to list and array.
If Not ListBox1.Items.Contains(Name) Then
ListCollection.Add(Item)
ListBox1.Items.Add(Name)
End If
End Sub

Private Sub RemoveItemFromList(ByVal Name As String)

'Check to see if it's there, and remove it from array, then list.
If ListBox1.Items.Contains(Name) Then
ListCollection.RemoveAt(ListBox1.Items.IndexOf(Name))
ListBox1.Items.RemoveAt(ListBox1.Items.IndexOf(Name))
End If
End Sub
End
Class

Hope this helps,

Dustin.

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 4
Hi,
Thanks Dustin_H for your detailed replay...
But can you please tell me what does the following mean ?
ListCollection.Add(Item)
Thanks again...
Regards...
Ahmedabugh at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 5
ListCollection is a arrayList.

At the start you'll see Private ListCollection As New ArrayList

ArrayList is a object that you can use to keep a collection of items.

Basically, my code creates a array of objects, and stays in sync with the listbox. When you remove something from the listbox using my remove function, it is also removed from the listconllection array.
This function is used to add a entry to the listbox, and the Item is the object you want to associate with it. It can be anything, a string, integer, point, any kind of object.



Private Sub AddItemToList(ByVal Name As String, ByVal Item As Object)

'Check to see if it's there already, then add it to list and array.
If Not ListBox1.Items.Contains(Name) Then
ListCollection.Add(Item)
ListBox1.Items.Add(Name)
End If
End Sub


Use this to remove a item from you listbox.
In this case, all you have to do is pass the text in the selected item of the listbox. (or any item really, as long as you pass the text). It will remove whatever object you set from the listCollection array.



Private Sub RemoveItemFromList(ByVal Name As String)

'Check to see if it's there, and remove it from array, then list.
If ListBox1.Items.Contains(Name) Then
ListCollection.RemoveAt(ListBox1.Items.IndexOf(Name))
ListBox1.Items.RemoveAt(ListBox1.Items.IndexOf(Name))
End If
End Sub



I suppose i should have written you a sub to get the value of assiciated to the listbox. here's the sub to get a value depending on the text you pass in.



Private Function GetItemFromList(ByVal Name As String) As Object
If ListBox1.Items.Contains(Name) Then
Return ListCollection.Item(ListBox1.Items.IndexOf(Name))
End If
Return Nothing
End Function


An exmaple of how you can use this kinf of setup.

Say you wanted to get the object assiciated with an item when you click on the list. This code would be used when selected index is changed.



Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
MsgBox(GetItemFromList(ListBox1.SelectedItem))
End Sub

Although i'm putting the value out to a msgbox, you can still declare a variable and use it.


Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddItemToList(
"Some Point", New Point(10, 5))
End Sub


Private
Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
Dim pos As Point
pos = GetItemFromList(ListBox1.SelectedItem)
MsgBox(pos.X &
"," & pos.Y)
End Sub


Dustin.

Dustin_H at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 6
Thanks again for your comrehensive explanation.

I got that we make a copy of values in a seperate array instead.

That helped a lot...

Regards
Ahmed

Ahmedabugh at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...
# 7

If you are trying to simulate itemdata in VB6 which enabled you to store a text and itemdata pair in the combo or listbox this is fairly easy.

The example here requires a ComboBox and Button on a form. This will add 3 items into the combo and when you select one and press the button it will display both the text and the value.

As the Items collection of the combo/list box can store any objects you want you can store any objects you have in you application to the listbox. The important thing to have in the class is the

Public Overrides Function ToString() As String
Return sString
End Function

Which will be used to return the default text to display in the control. This is actually much more powerful than itemdata was in VB as you can store any objects in the items collection and you are not limited to a single itemdata - but can reference any properties/fields/methods of the objects.
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'//Add items to the listbox

Dim x As ListItemPair

x = New ListItemPair("test", 1)
ComboBox1.Items.Add(x)

x = New ListItemPair("fred", 2)
ComboBox1.Items.Add(x)

x = New ListItemPair("bob", 3)
ComboBox1.Items.Add(x)
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
'//Display the text and value of the selected item.

'//The selected item is a the ListItemPair Object
Dim Obj As ListItemPair
Obj = ComboBox1.SelectedItem

'//Build a string just to show value and string
Dim strString As String
strString = Obj.ToString & " Value:" & CType(Obj.sValue, String)
MessageBox.Show(strString)
End Sub
End Class


Public Class ListItemPair
'//Could use Properties but simply using public fields for this example.
Public sString As String
Public sValue As Integer

Sub New(ByVal StringValue As String, ByVal Value As Integer)
sString = StringValue
sValue = Value
End Sub

Public Overrides Function ToString() As String
Return sString
End Function
End Class

spotty at 2007-9-9 > top of Msdn Tech,Visual Basic,Visual Basic General...