Results 1 to 4 of 4

Thread: Unable to decrement numeric LED in my ActiveX Control

  1. #1

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    4

    Unable to decrement numeric LED in my ActiveX Control

    Hi all,
    I am using VB 6.0, and want to use a Timer in order to decrement GMS NumLED from 3:00 to 0:00, I have tried, but program crashes after first run.
    So, I made a simple application in which I only included the NumLED, Timer, and a Start label. There are no errors, code compiles and runs fine, I have tried to debug it too, all program flow is as expected, but it doesn't decrement the NumLED. I would be very thankful for any suggestions, links or fix, or even a hint about, what could have gone wrong?
    I am attaching complete code here for reference:
    Code:
    ' simple program to decrement NUM LED timer using timer component
    Option Explicit
    
    ' Label start click event
    Private Sub lblStart_Click()
        Timer1.Enabled = True
    End Sub
    
    ' Timer tick event
    Private Sub Timer1_Timer()
        Static timeValue As Integer
        timeValue = 10
        Do While (timeValue <> 0)
            timeValue = DecrementNumLedTime(timeValue)
            NumLED1.Value = timeValue
        Loop
        Timer1.Enabled = False
    End Sub
    
    ' Decrementing NumLED timer
    Public Function DecrementNumLedTime(ByVal timeValue As Integer) As Integer
        Static quotient As Integer
        Static remainder As Integer
       
            remainder = timeValue Mod 60
            quotient = timeValue / 60
      
        DecrementNumLedTime = remainder - 1
    End Function

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Unable to decrement numeric LED in my ActiveX Control

    Welcome to VBForums

    Your DecrementNumLedTime seems to be more complex than needed, and not accurate either (eg: it ignores quotient). It seems to me that the only code required in it is this: DecrementNumLedTime = timeValue - 1 If that works, using a function is overkill, as you could simply do it inline instead.

    I also don't see the point of using a Timer as you have, as all it is doing is delaying the start of the process - what I suspect you wanted is to have a delay between the numbers, in which case you should remove the loop and re-organise the code a little.

    Here is what I think you wanted:
    Code:
    ' simple program to decrement NUM LED timer using timer component
    Option Explicit
    Private timeValue As Integer
    
    ' Label start click event
    Private Sub lblStart_Click()
        Timer1.Enabled = True
        timeValue = 10
    End Sub
    
    ' Timer tick event
    Private Sub Timer1_Timer()
        If timeValue <= 0 Then
          Timer1.Enabled = False
          Exit Sub
        End If
    
        timeValue = timeValue - 1
        NumLED1.Value = timeValue
    
    End Sub

  3. #3

    Thread Starter
    New Member
    Join Date
    Oct 2008
    Posts
    4

    Re: Unable to decrement numeric LED in my ActiveX Control

    Hello,
    Thanks a lot.
    It works now.
    One more thing though.
    I want to use alphanumeric value in NumLED as 3:00 it is supposed to decrease like 2:59, 2:58 and so on...That's why I used the quotient and remainder.
    How do I achieve that?
    Thanks.

  4. #4
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Unable to decrement numeric LED in my ActiveX Control

    I have no idea what kind of things NumLED allows, but assuming it does allow that kind of value, you can use something like this to show timeValue as you want:
    Code:
        NumLED1.Value = (timeValue \ 60) & ":" & Format(timeValue Mod 60, "00")
    Note that \ is integer division - the result will have no decimal places (so while 2/5 = 2.5, 2\5 = 2).

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