ListBox Binding and SelectedIndexChanged

A

afh3

Re: Windows Forms ListBox Binding and SelectedIndexChanged Event firings.

It would appear that the process of binding a ListBox to a DataSource and
setting the DisplayMember and ValueMember properties results in the
SelectedIndexChanged event firing for each of the property settings.

How remarkably annoying is that?

Private Sub btnGetImageList_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGetImageList.Click
Dim myDataTable As localhost.dsImageList.ImagesDataTable

myDataTable = mywebsvc.GetImageList.Images.Copy

ListBox1.DataSource = myDataTable

ListBox1.ValueMember = myDataTable.Image_IDColumn.ToString

ListBox1.DisplayMember = myDataTable.Image_NameColumn.ToString

ListBox1.SelectedIndex = -1

End Sub

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

Dim myImageBytes() As Byte = mywebsvc.GetImage(ListBox1.SelectedValue)

Dim myStream As New System.IO.MemoryStream(myImageBytes, 0,
myImageBytes.Length - 1)

Dim myImage As Image

pictDisplayImage.Image = myImage.FromStream(myStream)



End Sub


In the code above, the ListBox1_SelectedIndexChanged event fires for >each<
of the property settings in the button click event -- in addition to the one
I was expecting, where I set the SelectedIndex to -1. The SelectedIndex
value during all of the totally uncalled-for event firings is zero -- a
valid value for the index of a list member, which makes trapping it
remarkably inconvenient. Also, it seems to me that since the value at the
mis-fired events is always zero, it hasn't really changed, has it? Doesn't
that sort of defy the whole concept of the name of the event?

What's up with this? Let me guess....I see an answer...It's coming to
me...."Wait for Whidbey"

-afh3

Sorry for the tone. It's late and I'm p.o.'ed that I've wasted an hour on
this issue.
 
C

Cor

Hi Afh

It is a very known isue, it is annoying yes.

But the most simple remedy is to set a flag/bool/swich whatever you name it.
(That is mostly done)

In this case with binding, you can also look what the change committed can
do for you, but check that very good, in my expirience has that also strange
behaviours.

I hope this helps,

Cor
 
M

MS public

Actually, it fires twice. Once when the list is filled with the data and
once because the control received focus.

here is the solution
RemoveHandler ListBox1.SelectedIndexChanged, AddressOf
ListBox1_SelectedIndexChanged

ListBox1.DataSource = tab

ListBox1.DisplayMember = tab.Columns(0).ToString()

ListBox1.ValueMember = tab.Columns(0).ToString()

AddHandler ListBox1.SelectedIndexChanged, AddressOf
ListBox1_SelectedIndexChanged


Regards - OHM
 
R

Robin Tucker

It is annoying I agree, not just for data binding. I have to place a flag
"bIgnoreEvent" set to true when I change a selection and don't want the
event to fire.
 
A

afh3

Well, MS Public, maybe it actually fires twice for you, but when I run the
code below it actually fires exactly as I said it does -- once for each
property setting thank you. Not that it actually matters, but that is how
the code actually works on my machine anyway, actually.

I have worked around the issue with a simple boolean test but I still think
that the event needs to be renamed to something like
"SelectedListBoxHasHadSomePropertySetOrAnIndexChanged"

-afh3
 
A

afh3

Yeah, that's what I did too. The previous suggestion of removing the handler
and reassigning it seems, in my opinion, to look a little cleaner in the
code though. I will probably change to that approach.

Still just plain wrong of it to behave that way though.
-afh3
 

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