Combo Box SelectedValueChanged

G

Guest

Hi fill a combo box with items as the form loads but this calles the
SelectedValueCahnged event. I dont want it to call the event until I select a
value in the combo box, whats the best way around the problem.

I think I can get around it by adding the handler after I have populated the
data, but just wondering if there is a better solution.
 
M

Morten Wennevik

Hi Neil,

Just filling the ComboBox with values shouldn't fire any
SelectedValueChanged event. How are you filling it?
 
G

Guest

I basically read the values with a reader from SQL and then create a
datatable. I then make the combo box reference the table.

Example code.

Do while myReader.Read
Dim currentRow as DataRow = myTable.NewRow
currentRow("FieldID") = myReader("FieldID")
currentRow("FieldDisplayName") = myReader("FieldDisplayName")
myTable.Rows.Add(currentRow)
Loop

ME.cboTest.DataSource = myTable
Me.cboTest.DisplayMember = "FieldDisplayName"
Me.cboTest.ValueMember = "FieldID"


Thanks

Neil
 
M

Morten Wennevik

Interesting, it seems that ValueChanged fires whenever you set the
DataSource, DisplayMember or ValueMember. I'm not sure you can avoid
firing the event, but you can use a flag to determine when to not process
the event.


ignoreFlag = True
Me.cboTest.DataSource = myTable
Me.cboTest.DisplayMember = "FieldDisplayName"
Me.cboTest.ValueMember = "FieldID"
ignoreFlag = False


And in your event

If(ignoreFlag)
Return
End If
 
G

Guest

Alternatively, you could remove the handler of the event before you assign
the datasource and then reattach it after you're done.

So you can modify your code to do this:

'
' Remove the handler
'
RemoveHandler cboTest.SelectedValueChanged, AddressOf
cboTest_SelectedValueChanged
'
' Bind the data
'
Me.cboTest.DataSource = myTable
Me.cboTest.DisplayMember = "FieldDisplayName"
Me.cboTest.ValueMember = "FieldID"

'
' Re-attach the handler
'
AddHandler cboTest.SelectedValueChanged, AddressOf
cboTest_SelectedValueChanged
 
G

Guest

Yep I used the addhandler , just thought it was a bit strange that this event
is raised in this manner.

Thanks for all the replies


Rad said:
Alternatively, you could remove the handler of the event before you assign
the datasource and then reattach it after you're done.

So you can modify your code to do this:

'
' Remove the handler
'
RemoveHandler cboTest.SelectedValueChanged, AddressOf
cboTest_SelectedValueChanged
'
' Bind the data
'
Me.cboTest.DataSource = myTable
Me.cboTest.DisplayMember = "FieldDisplayName"
Me.cboTest.ValueMember = "FieldID"

'
' Re-attach the handler
'
AddHandler cboTest.SelectedValueChanged, AddressOf
cboTest_SelectedValueChanged
 
G

Guest

I suppose that even though you specify the datasource and the data members,
under the hood items are still being added to the combo box ergo the selected
value would change.

It is debatable if Microsoft should switch off this behavior. Personally I
don't think so -- perhaps there are other housekeeping routines that need to
be called when each item is added.

Though it would be nice if there was a property such as EnableEvents that
would control the raising of of events to our custom code ....
--


Bits. Bytes.
http://bytes.thinkersroom.com
 

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