Why does SelectedIndexChang­ed event fire?

  • Thread starter Thread starter Bruce D
  • Start date Start date
B

Bruce D

I have code in my tabcontrol.SelectedIndexChang­ed event. And I noticed the
event fires when my form loads. Why? I don't think the event should fire.
Didn't get much help reading from MS or Google...

Any insight?

-bruce duncan
 
Just a guess here but it may be firing because the index changes from
nothing to a valid index when it's created. If you want to work around it,
you could just include a module level variable indicating whether the form
has loaded or not and and only allow the code to execute if it has.

Steve
 
Bruce

This is in the top hundred of the most asked questions in the dotnet
newsgroup

Solutions:
the most simple one
create a public boolean what you set in the end of your loading to true and
use that in your selectedindexchange event to let that only do something
when that boolean is true

In fact better because you do this only once and than avoid that every time
testing
remove the handler in the begin of your loading and set it again at the end

This event fires whenever the index is changed. And that happens when the
datasource is loading. It changes as well when you change it inside the
event and than you can get very terrible recursive actions.

I hope this helps,

Cor
 
Bruce D said:
I have code in my tabcontrol.SelectedIndexChang­ed event. And I noticed
the
event fires when my form loads. Why? I don't think the event should
fire.
Didn't get much help reading from MS or Google...

\\\
Private Sub TabControl1_SelectedIndexChanged( _
ByVal sender As Object, _
ByVal e As EventArgs _
) Handles TabControl1.SelectedIndexChanged
Static WasHere As Boolean
If Not WasHere Then
WasHere = True
Else
MsgBox("Selected index changed!")
End If
End Sub
///
 

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