By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,165 Members | 1,403 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,165 IT Pros & Developers. It's quick & easy.

listboxes

P: n/a
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?

Nov 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
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.

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"Jan Warning" <ja*********@wanadoo.nl> schreef in bericht
news:uT**************@tk2msftngp13.phx.gbl...
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?

Nov 21 '05 #2

P: n/a
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.

"Peter Proost" <pp*****@nospam.hotmail.com> wrote in message
news:e1**************@TK2MSFTNGP10.phx.gbl...
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.

--
Programming today is a race between software engineers striving to build
bigger and better idiot-proof programs, and the Universe trying to produce
bigger and better idiots. So far, the Universe is winning.
"Jan Warning" <ja*********@wanadoo.nl> schreef in bericht
news:uT**************@tk2msftngp13.phx.gbl...
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?


Nov 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.