CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4

Thread: 20 Stopwatches

  1. #1
    Join Date
    May 2001
    Posts
    23

    20 Stopwatches

    Hay Sorry one more Question about timer I hope this one is last hehe my Qaustion is I want to use 20 Stop watches in my Form for this purpose should I Creat 20 timer and Write Code for Each one or there is some alternate way


  2. #2
    Join Date
    Jan 2000
    Location
    Olen, Belgium
    Posts
    2,477

    Re: 20 Stopwatches

    You can use a control array. Add a timer to the form, press ctrl+c ctrl+v and respond yes to the messagebox. Now you will see that each timer has the same name, just a different index. When you double click your timer, you will see that in the event, the index returns. This way you know which timer exactly fired. If you also create a control array of labels and buttons, you can use the correspomding indexes (like when pressing command1(7) will start timer1(7), and timer1(7) will update label1(7).

    Hope this makes sence

    Tom Cannaerts
    [email protected]

    Programming today is a race between software engineers striving to build bigger and better idot-proof programs, and the universe trying to produce bigger and better idiots. So far, the universe is winning -- Rich Cook
    Tom Cannaerts
    email: [email protected]
    www.tom.be (dutch site)

  3. #3
    Join Date
    May 2001
    Posts
    23

    Re: 20 Stopwatches

    ok Logic is Good but I need to run 20 timers at differennt time .... when i press comand one I should get time of timer 1 when i press 2 i should get time of timer 2 and timer 1 and 2 are diiff from each other
    Can U do one Favour just copy paste the code for me bcz iam confuse in Indexing how should I write code for timer2 ?


  4. #4
    Join Date
    Feb 2001
    Location
    Stamford CT USA
    Posts
    2,167

    Re: 20 Stopwatches

    Try to understand the code below:

    Form1 consists of:
    1. Timer control: named TimerCtrl: indexed 1,2,3
    2. Command button: named CommandCtrl: indexed 1,2,3
    3. Label control: named LabelCtrl: indexed 1,2,3


    private Sub Form_Load()
    ' init controls
    dim nInd as Integer
    for nInd = 0 to 2
    LabelCtrl(nInd).Caption = "0"
    CommandCtrl(nInd).Caption = "&Start Timer"
    TimerCtrl(nInd).Enabled = false
    TimerCtrl(nInd).Interval = (nInd+5) * (1000)
    next nInd
    End Sub

    private Sub CommandCtrl_Click(Index as Integer)
    ' the command button index corresponds to the timer control index
    if (TimerCtrl(Index).Enabled) then
    ' disable the timer
    TimerCtrl(Index).Enabled = false
    CommandCtrl(Index).Caption = "&Start Timer"
    else
    ' start the timer
    LabelCtrl(Index).Caption = "0"
    TimerCtrl(Index).Enabled = true
    CommandCtrl(Index).Caption = "&Stop Timer"
    end if
    End Sub

    private Sub TimerCtrl_Click(Index as Integer)
    ' add 1 to the appropriate label
    LabelCtrl(Index).Caption = CInt(LabelCtrl(Index).Caption) + 1
    End Sub




    Hope that you can figure out what the code does.
    -Cool Bizs

    Good Luck,
    -Cool Bizs

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

Featured