Exmaple.
ComboBox1.Items.Add("Test string") ListBox1.Items.Add("Test String") |
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. 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. ListCollection.RemoveAt(ListBox1.Items.IndexOf(Name)) ListBox1.Items.RemoveAt(ListBox1.Items.IndexOf(Name)) End If End Sub End Class |
Hope this helps,
Dustin.
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. 'Check to see if it's there already, then add it to list and array. 'Check to see if it's there, and remove it from array, then list. 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. Although i'm putting the value out to a msgbox, you can still declare a variable and use it.
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)
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)
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.
Private Sub ListBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListBox1.SelectedIndexChanged
MsgBox(GetItemFromList(ListBox1.SelectedItem))
End Sub
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.
I got that we make a copy of values in a seperate array instead.
That helped a lot...
Regards
Ahmed
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