Very Unpredictable ListBox Behaviour

A

alanb

This is driving me NUTS. I have a listbox bound to a datasource and it
all works fine until I start deleting items from the class collection.
The frustrating thing is that there is no pattern to the error and I
can't recreate the problem with any certainty. I refresh the listbox
after deleting a class item with the following code:

With lstBanks
.DataSource = Nothing
.DisplayMember = "Title"
.ValueMember = "BankID"
.DataSource = Banks
End With

Generally everything works fine - after a class item is deleted the
listbox displays the correct items using the refresh code above.
HOWEVER every now and again the listbox starts to fill itself with the
class name, i.e. repeated lines of 'MyProgram.Bank' (the number of
lines is always equivalent to the correct number of items that should
be displayed in the listbox). Then the program will crash when a
listbox item is clicked. It seems obvious to me that the DisplayMember
and ValueMember are not being properly set (or ignored) for some reason
(I have checked these values after the problem appears and they seem to
be set fine).

I have tried just about every combination of lines including refresh,
different order of lines etc. etc..

Any help would be very much appreciated.

Alan
 
C

Cor Ligthert [MVP]

Alanb,

Are you sure that this is not happen when you delete a selected item.
Than there is a selectedindex change event and that will be triggered.

I hope this helps,

Cor
 
A

alanb

Many thanks for the reply Cor,

I've added more protection to the events and yes it does seem to be
much more stable - it's even more difficult to reproduce the problem
now, BUT it still happens on rare occasions! When I create a new file
the program seems to run very well, however the problem seems to happen
only after I've stopped the program and run it again (although it still
very rarely happens and not immediately after loading the previous
data) .

I save my data via serialization rather than dumping to a database -
I'm beginning to wonder if this is causing a problem.

Alan
 

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