combobox fill causing SelectedIndexChanged without selecting

  • Thread starter Thread starter sparkle
  • Start date Start date
S

sparkle

Hi Everybody,

I'm filling a combobox from a class, which works fine on it's own.

But when I insert code to fill in other controls something in the
combobox fill is causing the SelectedIndexChanged event to happen
when the form (hence combobox) loads, not when I select something
from the combobox.

Do I have something in the wrong order? I've read that that sometimes
makes a difference...

Thanks for any suggestions,
Here's my code:

Dim ClassVehicleID As New ParkingPermits.BizLogic.Vehicle
VehicleInfo = ClassVehicleID.GetAllVehicles


With cboSearchCountyVehicle
' .DataSource = VehicleInfo
.DisplayMember = "VehicleID"
.ValueMember = "VehicleID"
.SelectedIndex = -1
End With


Private Sub cboSearchCountyVehicle_SelectedIndexChanged(ByVal
sender As Object, ByVal e As System.EventArgs) Handles
cboSearchCountyVehicle.SelectedIndexChanged

LoadPermitInfofromCountyVehicle()

End Sub
 
Hi,

Try setting selectedindex = -1 twice - there is a known bug in the framework
on this.

HTH,

Bernie Yaeger
 
Thanks Bernie,

I should have mentioned that I had already tried that earlier with no
change in results. I don't think it's the same bug. The one I saw was
that SelectedIndexChanged wasn't fired.

In my case it fires for no reason.

Still looking for a solution because I really want to use a combobox
for this!
 
Hi,

Well, when you set selectedindex = -1 you are probably firing the
selectedindexchanged event. So, let us see the code that runs when you call
LoadPermitInfofromCountyVehicle(). Something in there must be firing the
event again.

Bernie
 
Thanks for sticking with me, Bernie.

Here's what I found. SelectedIndexChanged is fired when a form loads
and when the selected index of the combobox is set to -1. So
potentially twice before you even select an item in the combobox. And
that it happens when you bind to a datasource.

Only found a couple of suggestions in articles, neither of which I
have gotten to work:

http://www.winnetmag.com/MobileWireless/Article/ArticleID/41825/MobileWireless_41825.html

http://www.knowdotnet.com/articles/protectevents.html

To try this, you just need a combobox that fills from a datasource on
form_load, then set the combo's selectedindexchanged to do something,
for instance, now I'm filling in a datagrid. Put your breakpoint on
the combobox fill and selectedIndexChanged event and you'll see.

For now, I'm doing something hokey; hiding the datagrid until after I
have selected something. I'd like a better workaround if you find
something out!
 
Hi Sparkle,

Well, the knowledgebase article really doesn't answer your need. I'm glad
you've found a workaround, but I'd still like to see the code that is called
inside the selectedindexchanged event.

Bernie
 
Ok, here you go. This workaround isn't great, so if we can figure out
a better way, I'll be glad:

Have you tried a test yourself? I didn't really see it having to do
with the code binding the AutosGrid, but fresh eyes will be
appreciated...

'SELECTED INDEX CHANGED code
Private Sub cboCountyVehicle_SelectedIndexChanged_1(ByVal sender
As System.Object, ByVal e As System.EventArgs) Handles
cboCountyVehicle.SelectedIndexChanged

if cboCountyVehicle.SelectedIndex >= 1 Then
BindAutosGridbyVehicleID()
end if

End Sub
' BIND AUTOS GRID code

Sub BindAutosGridbyVehicleID()
Dim ClassVehicleID As New ParkingPermits.BizLogic.Vehicle
Dim VehicleInfo As ParkingPermits.info.VehicleInfo()
VehicleInfo =
ClassVehicleID.GetVehicleByVehicleID(cboCountyVehicle.SelectedValue)
'If VehicleInfo.Length = 0 Then
' MessageBox.Show("Vehicle not found for " +
cboCountyVehicle.Text.Trim + ".", App.MessageCaption,
MessageBoxButtons.OK, MessageBoxIcon.Information)
'Else
_ArrayBinding = VehicleInfo
grdAutos.DataSource = _ArrayBinding

Dim grid As DataGridTableStyle = New DataGridTableStyle
grid.MappingName = _ArrayBinding.GetType().Name

Dim ColumnMake As New DataGridTextBoxColumn
With ColumnMake
.MappingName = "Make"
.HeaderText = "Make"
.Width = 70
End With

Dim ColumnModel As New DataGridTextBoxColumn
With ColumnModel
.MappingName = "model"
.HeaderText = "Model"
.Width = 90
End With

Dim ColumnLicense As New DataGridTextBoxColumn
With ColumnLicense
.MappingName = "license"
.HeaderText = "License"
.Width = 65
End With

Dim ColumnYear As New DataGridTextBoxColumn
With ColumnYear
.MappingName = "year"
.HeaderText = "Year"
.Width = 65
End With

Dim ColumnColor As New DataGridTextBoxColumn
With ColumnColor
.MappingName = "Color"
.HeaderText = "Color"
.Width = 60
End With

grid.GridColumnStyles.AddRange(New DataGridColumnStyle()
{ColumnMake, ColumnModel, _
ColumnLicense, ColumnYear, ColumnColor})
grdAutos.TableStyles.Add(grid)
grdAutos.TableStyles.Clear()
End Sub

#End Region 'autos grid
 

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

Back
Top