0

I am trying to make a rythmn game in visual basic that adds pictureboxes to a form every time the elapsed event of a system timer is raised. I have a separate sub which adds the picturebox to the form, and when I run the code, that sub is executed every time, including the code which adds the pictureboxes to the form, but the pictureboxes don't appear on the form. This was working fine when I used the normal timer component in the toolbox, but not with the systems.timers.timer. I think this might be because the timer is on a different thread, but I don't have any errors or warnings.

I have to use a system timer (I think) in order to have it more in sync with the music. The timer interval is set to 94ms for my test song, but will change depending on song bpm.

Seangole
  • 3
  • 1

2 Answers2

0

If the picture boxes work using the normal timer component then use that.

If you need to sync this with the music you could have something like this...

Dim musicStarted As Boolean = False
...

In your timer code you would have...

If musicStarted Then
   'Add new Picture Box
Else
   'Start Music
   musicStarted = True
   'Show first Picture Box
End If
Mych
  • 2,527
  • 4
  • 36
  • 65
0

System.Timers.Timer and System.Windows.Forms.Timer are two very different beasts

First one would usually be used in a console or service application, the latter one would be used in a windowed application which seems to be your case.

System.Timers.Timer would be added in this manner:

Dim tmr1 As System.Timers.Timer
...
tmr1 = New System.Timers.Timer
tmr1.AutoReset = True
tmr1.Enabled = False
tmr1.Interval = 3000
AddHandler tmr1.Elapsed, AddressOf tmr_Elapsed
tmr1.Start()

...

Private Sub tmr_Elapsed(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs) 

    Console.WriteLine("TICK")

End Sub

And for System.Windows.Forms.Timer:

Dim tmr2 As Windows.Forms.Timer
...
tmr2 = New Windows.Forms.Timer
tmr2.Interval = 2000
tmr2.Enabled = False
AddHandler tmr2.Tick, AddressOf tmr2_tick
tmr2.Start()
...
Private Sub tmr2_tick(ByVal sender As System.Object, ByVal e As System.EventArgs)

    Console.WriteLine("TICK2")

End Sub
Rok Jarc
  • 18,765
  • 9
  • 69
  • 124