Stopping Event Handlers on Startup

  • Thread starter Thread starter Ricky W. Hunt
  • Start date Start date
R

Ricky W. Hunt

Is there a way to prevent such events as TrackBar1.ValueChanged from firing
during initialization? I'm using a TrackBar control to control the volume of
media player app I wrote. The problem is the buffer for playback doesn't
exist until the user opens a media file. On startup the value of the slider
is set to -600 using the Designer. For some reason (because the value is not
zero???) this causes it to execute the "valuechanged" subroutine which tries
to apply volume changes to a non-existent buffer and throws an error.
There's a couple of things that happens like this in the program and so far
the only solution I've come up with is the ugly "If FirstTime Then...Else"
type of code. Is there a way to stop those events from triggering on
initialization or am I approaching this wrong?
 
Hi Ricky
A simple way to do it is to attach the event TrackBar1.ValueChanged to its
handler only after initialization.
So you write the handler ( without the handles key word)
As follows
Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ByVal e As
System.EventArgs)


End Sub

Then after you are done with initialization and you now want the event to
be fired normally , you just associate the handler ( that you already
created) with the event using the AddHandler keyword. May be you want to
do that when the form load ( or after that depending on the logic of your
program ) as follows
Private Sub Form1_Load(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Load
AddHandler TrackBar1.ValueChanged, AddressOf TrackBar1_ValueChanged
End Sub
Hope that was clear

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 
Mohamoss said:
Hi Ricky
A simple way to do it is to attach the event TrackBar1.ValueChanged to its
handler only after initialization.
So you write the handler ( without the handles key word)
As follows
Private Sub TrackBar1_ValueChanged(ByVal sender As Object, ByVal e As
System.EventArgs)


End Sub

Wow. I see how that would work but there seems like there would be a more
graceful, "global" solution offered by the language. OK. Thanks.
 
Hi
In fact this is the solution offer by the language; the keyword Addhandler
is offered by the language is just to combine an event with its delegate
handler at runtime

Mohamed Mahfouz
MEA Developer Support Center
ITworx on behalf of Microsoft EMEA GTSC
 

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