Jump to content
  • Advertisement
Sign in to follow this  

simple vb.net timer question

This topic is 4890 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Here is my program codes:
Public Class main
    Inherits System.Windows.Forms.Form

#Region " Windows Form Designer generated code "

    Public Sub New()

        'This call is required by the Windows Form Designer.

        'Add any initialization after the InitializeComponent() call

    End Sub

    'Form overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
            End If
        End If
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(292, 273)
        Me.FormBorderstyle = System.Windows.Forms.FormBorderstyle.Fixed3D
        Me.MaximizeBox = False
        Me.Name = "main"
        Me.Text = "Demo"
        Me.WindowState = System.Windows.Forms.FormWindowState.Maximized

    End Sub

#End Region

    Private mainMenu As System.Windows.Forms.MainMenu
    Private mnuGame As System.Windows.Forms.MenuItem
    Private mnuGameNew As System.Windows.Forms.MenuItem
    Private mnuGameDiv As System.Windows.Forms.MenuItem
    Private WithEvents mnuGameExit As System.Windows.Forms.MenuItem

    Private grid(,) As Integer
    Private preview() As Integer
    Private score As Integer

    Private gridBox(,) As System.Windows.Forms.PictureBox
    Private previewBox() As System.Windows.Forms.PictureBox
    Private scoreTitleLabel As System.Windows.Forms.Label
    Private scoreLabel As System.Windows.Forms.Label

    Private timer As System.Timers.Timer

    Private Sub main_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    End Sub

    Shared Sub Main()
        Application.Run(New main)
    End Sub

    Private Sub main_Closed(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.Closed
    End Sub

    Private Sub addMenu()
        mainMenu = New System.Windows.Forms.MainMenu
        mnuGame = New System.Windows.Forms.MenuItem("Game")
        mnuGameNew = New System.Windows.Forms.MenuItem("New")
        mnuGameDiv = New System.Windows.Forms.MenuItem("-")
        mnuGameExit = New System.Windows.Forms.MenuItem("Exit")
        Menu = mainMenu
    End Sub

    Private Sub createGrid()
        Erase grid
        ReDim grid(9, 9)
        Dim heightIndex As Integer
        Dim widthIndex As Integer
        For heightIndex = 0 To UBound(grid, 1) - 1
            For widthIndex = 0 To UBound(grid, 2) - 1
                grid(heightIndex, widthIndex) = 0
    End Sub

    Private Sub createPreview()
        Erase preview
        Erase previewBox
        ReDim preview(2)
        ReDim previewBox(2)
    End Sub

    Private Sub resetScore()
        score = 0
    End Sub

    Private Sub startTimer()
        timer = New System.Timers.Timer
        AddHandler timer.Elapsed, AddressOf timerTriggered
        timer.Interval = 1000
    End Sub

    Private Sub subMnuGameExit(ByVal sender As Object, ByVal e As EventArgs) Handles mnuGameExit.Click
    End Sub

    Private Sub main_Paint(ByVal sender As Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
        Dim bitmap As System.Drawing.Bitmap
        bitmap = New Bitmap(Me.ClientRectangle.Width, Me.ClientRectangle.Height)
        Dim graphics As System.Drawing.Graphics
        graphics = graphics.FromImage(bitmap)
        Dim boxPen As System.Drawing.Pen
        boxPen = New Pen(System.Drawing.Color.Gray, 3)
        Dim rectangle As System.Drawing.Rectangle
        rectangle = New System.Drawing.Rectangle(CInt(0.3 * (Me.ClientRectangle.Width - 1)), CInt(0.1 * (Me.ClientRectangle.Height - 1)), CInt(0.5 * (Me.ClientRectangle.Width + 1)), CInt(0.8 * (Me.ClientRectangle.Height + 1)))
        Me.CreateGraphics.DrawRectangle(boxPen, rectangle)
        Dim heightIndex As Integer
        For heightIndex = 0 To 2
            preview(heightIndex) = 0
            previewBox(heightIndex) = New System.Windows.Forms.PictureBox
            previewBox(heightIndex).Width = CInt(0.1 * (Me.ClientRectangle.Width - 1))
            previewBox(heightIndex).Height = CInt(0.1 * (Me.ClientRectangle.Height - 1))
            previewBox(heightIndex).Left = CInt(0.05 * (Me.ClientRectangle.Width + 1))
            previewBox(heightIndex).Top = CInt(0.1 * (Me.ClientRectangle.Height + 1) + heightIndex * previewBox(heightIndex).Height)
            previewBox(heightIndex).BackColor = System.Drawing.Color.Black
        scoreTitleLabel = New System.Windows.Forms.Label
        scoreTitleLabel.Width = CInt(0.1 * (Me.ClientRectangle.Width - 1))
        scoreTitleLabel.Height = CInt(0.1 * (Me.ClientRectangle.Height - 1))
        scoreTitleLabel.Left = CInt(0.8 * (Me.ClientRectangle.Width + 1))
        scoreTitleLabel.Top = CInt(0.1 * (Me.ClientRectangle.Height + 1))
        Dim family As System.Drawing.FontFamily
        family = New System.Drawing.FontFamily("Microsoft Sans Serif")
        Dim size As Integer
        size = 12
        scoreTitleLabel.AutoSize = True
        scoreTitleLabel.BackColor = System.Drawing.Color.Transparent
        scoreTitleLabel.Font = New System.Drawing.Font(family, size)
        scoreTitleLabel.Text = "Score"
    End Sub

    Private Sub timerTriggered(ByVal sender As Object, ByVal e As System.Timers.ElapsedEventArgs)
        MsgBox("timer triggered")
    End Sub

End Class
The messagbox with the timer's interval shows, but the timer never triggers. Why does this happen? Also, should I add events to my menus using WithEvents and use event registering with my timer or is there a good way to handle these both in the same way?

Share this post

Link to post
Share on other sites
Guest Anonymous Poster
Well, concerning your Timer not being triggered - you have to
start it after setting it up. Just call Timer1.Start() after setting it up ...

Private Sub startTimer()
timer = New System.Timers.Timer
AddHandler timer.Elapsed, AddressOf timerTriggered
timer.Interval = 1000
timer.Start() ' *** Just add this line ***
End Sub

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!