listboxes

J

Jan Warning

How can i refresh the contents of a listbox.

On my form I have a listbox.
In the Form_load sub I coded:
listbox.datasource = A (where A is a collection that is populated from a
diskfile at program start)
listbox.displaymember = "Naam"

When the form is displayed the listbox shows the items in the collection.

So far no problem.

Now I add an item to the collection A and want that to show up in my
listbox.
When I do a A.Items.Add() I get a runtime error message "Cannot modify the
Items collection when the DataSource property is set."
When i change the DataSource property to Nothing and than do the Items.Add I
get an empty listbox. What am I doing wrong?
 
P

Peter Proost

This works for me:

Private strValues as new Arraylist
'in the load I fill a arraylist with values from a file
Try
Dim objRead As New IO.StreamReader("c:\values.txt")
Do While objRead.Peek > -1
strValues.Add(objRead.ReadLine)
Loop
objRead.Close()
ListBox1.DataSource = strValues
Catch ex As Exception
MsgBox(ex.ToString)
End Try

'then behind a button I've got code to add a value
strValues.Add("NewValue")
ListBox1.DataSource = Nothing
ListBox1.DataSource = strValues


hth greetz Peter, be aware there's no check if the file exists or anything.
 
J

Jan Warning

Thanks.
I tried that already, but it resulted also in an empty listbox.
When I tried it again, I happened to detect that the listbox is not really
empty, because when I click on it, corresponding data shows up in a second
listbox.
So, the proper information is in the listbox but is not shown for some
reason.
Absolutely no idea why.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top