|
-
May 23rd, 2012, 04:08 AM
#1
Thread Starter
Addicted Member
Create Array Timer
Hi, Please help. I have this current problem I created user control with timer and use it on my form. My problem is how can I create array timer bound to user control.
example my user control name is uc_project my timer g_tmr
On my main form
uc_project.g_tmr.Enabled = true working fine
But how to make uc_project.g_tmr(index).enabled = true to create multiple timer.
-
May 23rd, 2012, 04:18 AM
#2
Re: Create Array Timer
vb.net Code:
Public Class SomeClass Private _timers As New List(Of Timer) Public ReadOnly Timers() As List(Of Timer) Get Return _timers End Get End Property End Class
That pattern is used throughout the .NET Framework, e.g. Control.Controls, DataSet.Tables, DataTable.Rows, DataTable.Columns, ComboBox.Items, ListBox.Items, ListView.Items, DataGridView.Rows, DataGridView.Columns, DataGridViewRow.Cells, etc, etc.
-
May 23rd, 2012, 04:31 AM
#3
Thread Starter
Addicted Member
Re: Create Array Timer
Thank you, I will check on this...
-
May 23rd, 2012, 04:54 AM
#4
Thread Starter
Addicted Member
Re: Create Array Timer
I get and error Get syntax error, End Property must be preceded by matching property
-
May 23rd, 2012, 05:04 AM
#5
Re: Create Array Timer
Sorry. That's what I get for not using an IDE.
vb.net Code:
Public Class SomeClass
Private _timers As New List(Of Timer)
Public ReadOnly Property Timers() As List(Of Timer)
Get
Return _timers
End Get
End Property
End Class
-
May 23rd, 2012, 06:38 AM
#6
Re: Create Array Timer
It's a good practice to expose the collection as an IList, rather than just a List:
vbnet Code:
Public Class SomeClass Private _timers As New List(Of Timer) Public ReadOnly Property Timers() As IList(Of Timer) Get Return _timers End Get End Property End Class
You may not be at the level of programming where the benefits of this approach are of any value to you, but it's a good habit to get into IMO.
-
May 23rd, 2012, 11:13 AM
#7
Re: Create Array Timer
Actually, I think its better to have return types be concrete types. IList is better suited for passing in parameters because you get the ability to pass all types of lists including arrays. With IList as a return type you don't really get such a flexibility. I used to use this practice of returning ILists until I realized it introduced no additional flexibility.
-
May 23rd, 2012, 09:23 PM
#8
Thread Starter
Addicted Member
Re: Create Array Timer
Hi guys thank you so much for all your help, Due to still on stage of learning vb.net 2010 I'm having hard time creating above example. I write another code to create timers at runtime however I'm having trouble to stop specific timer. Below is the new code workin fine generating timer. For example i created 10 timers how to stop timer 5 or timer 7 without affecting other timer?
Code:
Public Class Form1
Private dict As New Dictionary(Of Timer, Label)()
Private Sub CreateTimers()
Dim input As String = TextBox2.Text
Dim n As Integer = Convert.ToInt32(input)
For i As Integer = 1 To n
Dim timer As New Timer()
AddHandler timer.Tick, New EventHandler(AddressOf timer_Tick)
timer.Interval = (1000) * (i)
Dim label As New Label()
label.Name = "label" & i
label.Location = New Point(100, 100 + i * 30)
label.TabIndex = i
label.Visible = True
Me.Controls.Add(label)
dict(timer) = label
timer.Enabled = True
timer.Start()
Next
End Sub
Private Sub timer_Tick(ByVal sender As Object, ByVal e As EventArgs)
Dim t As Timer = DirectCast(sender, Timer)
Dim current As DateTime = DateTime.Now
dict(t).Text = String.Format("{0:00}:{1:00}:{2:00}", current.Hour, current.Minute, current.Second)
End Sub
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
CreateTimers()
End Sub
End Class
-
May 23rd, 2012, 09:38 PM
#9
Re: Create Array Timer
The Keys property of the Dictionary is a collection of just the Keys, i.e. the Timers in your case. You can index that collection to access a specific Timer.
By the way, please give your variables descriptive names. How about 'labelsByTimer' rather than 'dict', to describe what the value is actually for?
-
May 24th, 2012, 12:36 AM
#10
Thread Starter
Addicted Member
Re: Create Array Timer
jmcilhinney thanks for the reply can you show me some code on how to stop/start specific timer base on index?
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
|