By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,733 Members | 940 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,733 IT Pros & Developers. It's quick & easy.

combobox fill causing SelectedIndexChanged without selecting

P: n/a
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

Nov 21 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Hi,

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

HTH,

Bernie Yaeger

"sparkle" <hr****@hra.co.santa-cruz.ca-dot-us.no-spam.invalid> wrote in
message news:41**********@Usenet.com...
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

Nov 21 '05 #2

P: n/a
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!

Nov 21 '05 #3

P: n/a
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

"sparkle" <hr****@hra.co.santa-cruz.ca-dot-us.no-spam.invalid> wrote in
message news:41********@Usenet.com...
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!

Nov 21 '05 #4

P: n/a
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/MobileWirel...ess_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!

Nov 21 '05 #5

P: n/a
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

"sparkle" <hr****@hra.co.santa-cruz.ca-dot-us.no-spam.invalid> wrote in
message news:41********@Usenet.com...
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/MobileWirel...ess_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!

Nov 21 '05 #6

P: n/a
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(cboCountyVehi cle.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

Nov 21 '05 #7

P: n/a
Hi Sparkle,

I'm afraid I see nothing; sorry. I will keep looking, though.

Bernie

"sparkle" <hr****@hra.co.santa-cruz.ca-dot-us.no-spam.invalid> wrote in
message news:41**********@Usenet.com...
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(cboCountyVehi cle.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

Nov 21 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.