|
-
Jan 14th, 2004, 08:27 PM
#1
Thread Starter
Lively Member
Timer1 control array to Redim preserve
I have a timer control which I want to create an array of. Then at some point in my program I will need to Redim preserve that control array.
How would I do this?
Right now I have placed a timer control on my form then made a copy of it to create an array. When I try this line of code:
Code:
Redim preserve Timer1(2 to 10)
it's telling me "variable not defined", referring to the timer control.
I believe I need to create a new variable and set it equal to the Timer control array, but not sure how to code this.
?????
"never trouble trouble til trouble troubles you"
-
Jan 14th, 2004, 09:14 PM
#2
The picture isn't missing
to make arrays, use the Load() function, ie
dim IndexToLoad as integer
IndexToLoad = Timer1.UBound + 1
Load Timer1(IndexToLoad)
Timer1(IndexToLoad).interval=500
Remember, if someone's post was not helpful, you can always rate their post negatively  .
-
Jan 14th, 2004, 10:01 PM
#3
Thread Starter
Lively Member
If I needed to Redim Preserve this Timer control, how can I do this? It's asking for a defined variable instead of Timer1.
"never trouble trouble til trouble troubles you"
-
Jan 14th, 2004, 10:07 PM
#4
If you need more that one timer you should reconsider your design. Why do you feel you need more?
-
Jan 14th, 2004, 10:19 PM
#5
Thread Starter
Lively Member
Let's say I'm monitoring students while they take a test. Each student has a fixed amount of time to take the test. The catch is, students can arrive at different times. So each time a student arrives I will load a new timer array element to keep track of their test time. Then as a student finishes, I unload that timer and Redim Preserve the array.
I know this sounds a little weird but I'm determined to get this to work using a timer control array.
"never trouble trouble til trouble troubles you"
-
Jan 14th, 2004, 10:26 PM
#6
Add a timer to your form and set it's Index property to one. Then do
VB Code:
Private Sub Command1_Click()
Load Timer1(Timer1.Count)
End Sub
There is no way to Redim a control array. You should simply Unload those you don't need. In other words when the 2nd student finishes you would do
Unload Timer1(1)
-
Jan 14th, 2004, 10:56 PM
#7
Thread Starter
Lively Member
That's what I was afraid you would say. I'm able to load the timers and unload them. I was just uncomfortable with having my array "chopped" up.......elements being (0, 2, 3, 5, 9, etc..) as I continued to unload and load more timers.
By using the timer count property, will it keep incrementing when a new timer is loaded, no matter how many are unloaded during that period. In other words, let's say my timer count is at 7 and I unloaded 3 timers, is my timer count still 7? Or does it decrement as timers are unloaded?
"never trouble trouble til trouble troubles you"
-
Jan 15th, 2004, 01:08 AM
#8
The picture isn't missing
as long as you code right, that shouldn't be a problem unless you plan to have more than 32767 students per day. you COULD perhaps place it into a collection:
VB Code:
Dim col As New Collection
Private Sub Form_Load()
Load Timer1(Timer1.UBound + 1) 'load a new timer
col.Add Timer1(Timer1.UBound) 'add that new timer to the collection
col.Item(1).Interval=1000 'to reference it
'and then when you want to unload the timer:
unload col.item(1)
col.remove 1
End Sub
you still need to UNLOAD the timer before removing it from the collection
Remember, if someone's post was not helpful, you can always rate their post negatively  .
-
Jan 15th, 2004, 11:06 AM
#9
Originally posted by Jason Carden
That's what I was afraid you would say. I'm able to load the timers and unload them. I was just uncomfortable with having my array "chopped" up.......elements being (0, 2, 3, 5, 9, etc..) as I continued to unload and load more timers.
By using the timer count property, will it keep incrementing when a new timer is loaded, no matter how many are unloaded during that period. In other words, let's say my timer count is at 7 and I unloaded 3 timers, is my timer count still 7? Or does it decrement as timers are unloaded?
The Count property will reflect the total number of timers that exist at any. For example if you have timer1 objects with indexes of 0, 2, 3, 5 and 9 the Count would be 5.
If you want to delete all the added timers (you can't delete the zeroeth timer1) then you can do
VB Code:
Dim ctl As Control
For Each ctl In Controls
If TypeOf ctl Is Timer Then
If ctl.Index <> 0 Then
Unload ctl
End If
End If
Next
-
Jan 15th, 2004, 11:26 AM
#10
Thread Starter
Lively Member
Buggyprogrammer, thanks for the example of the collection. I believe this still doesn't solve my problem of "renumbering" all the elements or items into a consecutive line of numbers...1, 2, 3, 4, 5...etc, without losing my data.
I did read that it wasn't necessary to Redim a collection, but I don't understand why that is?? What's the alternative?
Martin, if my controls were different, not just a Timer, that would definitely come in handy....pretty slick stuff!!
"never trouble trouble til trouble troubles you"
-
Jan 15th, 2004, 11:55 AM
#11
When you remove elements from a collection no "hole" is left as it is when you remove elements from a control array.
-
Jan 15th, 2004, 12:07 PM
#12
Thread Starter
Lively Member
Hmmm...I ran the example Buggy.. supplied. I loaded 5 timers, index's were 1 - 5, but when I removed index 2, what was left running were 1, 3, 4 & 5!!!???
Here's what I wrote:
Code:
Private col As New Collection
Private x As Integer
Private Sub Command1_Click() 'unload timer
Unload col.Item(2)
col.Remove 2
End Sub
Private Sub Command2_Click() 'load timer
Dim a As Integer
a = Timer1.UBound + 1
Load Timer1(a) 'load a new timer
Timer1(a).Enabled = True
col.Add Timer1(a) 'add that new timer to the collection
col.Item(a).Interval = 5000 'to reference it
End Sub
Private Sub Timer1_Timer(Index As Integer)
x = x + 1
Text1.Text = x
End Sub
"never trouble trouble til trouble troubles you"
-
Jan 15th, 2004, 02:04 PM
#13
VB Code:
Private Function FindFree_Timer As Integer
Dim intIterate As Integer
FindFree_Timer = 0
On Error Goto ErrorHandler
For intIterate = 1 To Timer1.Ubound
Timer1(intIterate).Tag = Timer1(intIterate).Tag
Next
On Error Goto 0
'FindFree_Timer = 0 since all are loaded
Exit Function
ErrorHandler:
'Goes here if instance does not exist
FindFree_Timer = intIterate
Err.Clear
End Sub
Public Function Assigned_TimerIndex As Integer
Dim intRetIndex As Integer
intRetIndex = FindFree_Timer
If intRetIndex = 0 Then
Load Timer1(Timer1.Ubound + 1)
Timer1(Timer1.Ubound).Interval = 5000 'Ubound updates
Assigned_TierIndex = Timer1.Ubound
Else
Load Timer1(intRetIndex)
Timer1(intRetIndex).Interval = 5000
Assigned_TierIndex = intRetIndex
End if
End Sub
That should fill up your holes. Admittedly, they won't be in order of creation.... but do you really want to bother with that? In the end, you wont have miore than 32K students being assigned timers anyway.
You can modify the concept to skip over the holes if your iterating through the control array. Either create a list of loaded indices or use On Error Resume Next to ignore the error with unloaded indices.
Last edited by leinad31; Jan 15th, 2004 at 02:08 PM.
-
Jan 15th, 2004, 02:08 PM
#14
Frenzied Member
I can't figure out why you need more than one timer. All you need to do is have one timer that updates each second, then check an array of starttimes to see if the desired time has expired or not. Wouldn't that be a hell of a lot easier and faster???
-
Jan 15th, 2004, 02:09 PM
#15
Originally posted by ae_jester
I can't figure out why you need more than one timer. All you need to do is have one timer that updates each second, then check an array of starttimes to see if the desired time has expired or not. Wouldn't that be a hell of a lot easier and faster???
So true
-
Jan 15th, 2004, 02:31 PM
#16
Originally posted by Jason Carden
Hmmm...I ran the example Buggy.. supplied. I loaded 5 timers, index's were 1 - 5, but when I removed index 2, what was left running were 1, 3, 4 & 5!!!???
Why the confusion? You removed index 2 so now it's gone.
-
Jan 15th, 2004, 04:01 PM
#17
Thread Starter
Lively Member
No confusion, I guess. When you said it removes the "hole" I thought it was was going to renumber the elements/items into consecutive order.
Oh well, ...preciate all your help.
"never trouble trouble til trouble troubles you"
-
Jan 15th, 2004, 04:08 PM
#18
A collection will "renumber" itself but not any type of array.
-
Jan 15th, 2004, 04:21 PM
#19
Thread Starter
Lively Member
OK, again confused! Earlier when I ran the collection test app and removed item (2), the items that remained were 1, 3, 4 and 5. I was expecting it to be 1, 2, 3, 4!!
"never trouble trouble til trouble troubles you"
-
Jan 15th, 2004, 05:28 PM
#20
I'm a fan of collections but I'm not sure what advantage there is in this case. I modified buggy's code as shown.
VB Code:
Private Sub Form_Load()
Load Timer1(Timer1.UBound + 1) 'load a new timer
col.Add Timer1(Timer1.UBound) 'add that new timer to the collection
col.Item(1).Interval = 1000 'to reference it
Load Timer1(Timer1.UBound + 1) 'load a new timer
col.Add Timer1(Timer1.UBound) 'add that new timer to the collection
col.Item(2).Interval = 1000 'to reference it
Load Timer1(Timer1.UBound + 1) 'load a new timer
col.Add Timer1(Timer1.UBound) 'add that new timer to the collection
col.Item(3).Interval = 1000 'to reference it
'and then when you want to unload the timer:
Unload col.Item(2)
col.Remove 2
End Sub
If you put a breakpoint at the Unload line you will find that at that point you have three members of the collection and if you type the following in the immediate window
?col(1).Index
?col(2).Index
?col(3).Index
you'll get 1, 2, 3 which are the indexes of the control array and not the item number of the control array. If you let the code go to End Sub and you do the same thing in the immediate window you will find that you have only ?col(1).Index will say "1", while col(2).Index will say 3. In other words the collection hole has been filled but the control array (2) has been deleted and there still is a hole in that array.
-
Jan 15th, 2004, 09:31 PM
#21
Thread Starter
Lively Member
Clear as mud!!!...no, just kidding. I don't think we can get better proof than that....eh.
Thanks for deciphering this for me!!
"never trouble trouble til trouble troubles you"
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|