Results 1 to 5 of 5

Thread: Using a variable in Timer name

  1. #1

    Thread Starter
    New Member
    Join Date
    Jan 2018
    Posts
    3

    Using a variable in Timer name

    This is basic I know but I'm stumped

    I'm working on an app that has multiple buttons on the screen. The program is used on a touchscreen so I have a timer that flashes each active button. I need to be able to call each buttons timer to turn it off or on depending on if the pause button has been pressed for the program. The pause button would override the individual buttons time in a sense. I'm sure there may be a better way to do the button flashes without having a timer for each but I haven't figure that out.

    I have a variable (ButtonSelected) that is an Integer. It gets set when 1 of 24 buttons is pressed. There's only 1 active button at a time. I simply want to call a timer based on what button is active. Something like

    TmrBtn(ButtonSelected).enable = False

    Any guidance is appreciated.

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Using a variable in Timer name

    You really ought to consider redesigning this such that there is only one timer. It's possible. In your case, you'd just have to know which button should be flashed, but you DO know that, because you have that ButtonSelected variable. So, the Tick event of the timer can just flash whichever button was selected. If you use multiple timers, they could end up causing you trouble. From your description, I think it is unlikely that you'd end up with that kind of trouble, but order of execution can get confused with multiple timers. I won't say that they'll conflict with each other, but I won't say that they won't, either.

    After re-reading your post, it sounds like multiple buttons can be flashed, which is also possible with just one timer, even if you want to flash them at different rates.

    Also, in standard WinForms, there likely isn't a better way to flash a button than by using a timer.
    My usual boring signature: Nothing

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

    Re: Using a variable in Timer name

    Create an array of the timers, eg:
    Code:
    Private TmrBtn() as Timer = {TmrBtn1, TmrBtn2, ... , TmrBtn24}
    ...and then you can refer to each of them as you wanted.

    Note that using a single timer as suggested above could well be the better option.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,038

    Re: Using a variable in Timer name

    Another option would be to add the timer to the button. The .Tag property of a button is essentially a storage area that can hold anything...including a timer. So, you could assign each timer to the .Tag property of the button, then get it back with:

    Dim yourTimer = DirectCast(yourButtonHere.Tag,Timer)

    However, I still prefer having only a single timer.
    My usual boring signature: Nothing

  5. #5
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,764

    Re: Using a variable in Timer name

    Quote Originally Posted by Shaggy Hiker View Post
    ...However, I still prefer having only a single timer.
    Great advice!
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Tags for this Thread

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