Question : Combobox Databinding Question

Good Day Everyone,

I'm loading a combobox as such:

cmb.DataSource = dataset.table("TableName")
cmb.DisplayMember = "Company"
cmb.ValueMember = "CompanyId"

My question is this:  Whenever I put code in the combobox valuechanged event, the code I place in there is fired off 3 times, even if  no value is selected.  

I have a line in my code that grabs the selectedvalue of the item selected:

dim intCompanyID as inetger = cboCompanyName.SelectedValue

Before an items is selected, this bombs twice, but then succeeds the third time.  This code runs once for each line in the binding sequence.  Hope this makes sense....

How do I fix this to only fire the code when a item is selected and not when I'm loading the

Thanks.

jb

Answer : Combobox Databinding Question

Ok it looks like you are not using the original code so I'm going to use your new code and show you the changes.

If you just double click a ComboBox, it will auto create code similar to this:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

End Sub


Then you are adding your binding code so it would look something like this:

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

ComboBox1.DataSource = dataset.table("TableName")
ComboBox1.DisplayMember = "Company"
ComboBox1.ValueMember = "CompanyId"

End Sub


This produces the error you are experiencing.

Now change it to:


Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

ComboBox1.DataSource = dataset.table("TableName")
ComboBox1.DisplayMember = "Company"
ComboBox1.ValueMember = "CompanyId"

AddHandler ComboBox1.SelectedIndexChanged, AddressOf ComboBox1_SelectedIndexChanged
End Sub

And it should work.  Good luck!
Random Solutions  
 
programming4us programming4us