Results 1 to 6 of 6

Thread: [RESOLVED] Problem utilizing a Timer Control.

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2024
    Posts
    7

    Resolved [RESOLVED] Problem utilizing a Timer Control.

    Greetings everyone.
    I have a small demo program that, after clicking a button is attempting to assign a different string to each of 4 different label's text property. The aim is to have each label's text property show the newly assigned string 2 seconds after the previous assignment. The program executes without error but the 4 labels show the new assignments simultaneously instead of after the desired 2 second separation. Any assistance will be greatly appreciated.

    The code follows:

    Imports System

    Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    Timer1.Interval = 2000
    Timer1.Stop()
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
    Dim DemoRay() As String
    DemoRay = New String() {"A", "B", "C", "D"}
    Lbl1.Text = DemoRay(0)
    Lbl2.Text = DemoRay(1)
    Lbl3.Text = DemoRay(2)
    Lbl4.Text = DemoRay(3)
    Timer1.Stop()
    End Sub
    End Class

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,709

    Re: Problem utilizing a Timer Control.

    The application is doing exactly what you are telling it to do, after two seconds the Tick event fires and you are assigning a value to all 4 labels. If you want to do this every two seconds then you will need to only update one label and track which label you last updated.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,686

    Re: Problem utilizing a Timer Control.

    This is a case of writing code without actually knowing what that code is supposed to do. As indicated, all four Labels are being updated at the same time because that's what you wrote code to do. You need to put some thought into the logic first, then write code to implement that logic. If you want to update one Label on each Tick then it should be obvious that your Tick event handler needs to co9ntain code that only updates one Label, not four. Here's the basics of one possible option:

    1. Add all the Labels to a list.
    2. Store the index of the next Label to be updated in a variable.
    3. On Tick, update the Label at that index in the list and increment the index variable.
    4. If the index variable has gone beyond the end of the list, stop the Timer or reset the index as desired.

    You should already understand the actual steps involved in what you want to accomplish before writing any code to actually do it. That way, you won't end up with code that does something completely different.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    New Member
    Join Date
    Jul 2024
    Posts
    7

    Re: Problem utilizing a Timer Control.

    Thanks for the assistance. I now have better idea of how the timer control works.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,686

    Re: [RESOLVED] Problem utilizing a Timer Control.

    For the record, there is no Timer control. In order to be used in the designer, a class must implement the IComponent interface. That is usually done by inheriting the Component class, which provides a default implementation of that interface. The Control class inherits Component and then each individual control class inherits Control, either directly or indirectly. Timer inherits Component but not Control, thus it is a component but not a control.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    PowerPoster SamOscarBrown's Avatar
    Join Date
    Aug 2012
    Location
    NC, USA
    Posts
    9,500

    Re: [RESOLVED] Problem utilizing a Timer Control.

    I'm brand new at this .Net stuff, but spent almost three decades in VB6.

    This is 'my' solution...which may not be THE best one...the experts in .Net can tell you that.

    Code:
        Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
            Static inum As Integer
            Dim DemoRay() As String
            DemoRay = New String() {"A", "B", "C", "D"}
            If inum = 3 Then Timer1.Enabled = False
            astLabel(inum).Text = DemoRay(inum)
            inum = inum + 1
        End Sub
    EDIT: 'called' from a btn Click event setting Timer1.Enabled = True
    Sam I am (as well as Confused at times).

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width