Results 1 to 10 of 10

Thread: Create Array Timer

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    199

    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.

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Create Array Timer

    vb.net Code:
    1. Public Class SomeClass
    2.  
    3.     Private _timers As New List(Of Timer)
    4.  
    5.     Public ReadOnly Timers() As List(Of Timer)
    6.         Get
    7.             Return _timers
    8.         End Get
    9.     End Property
    10.  
    11. 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.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    199

    Re: Create Array Timer

    Thank you, I will check on this...

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    199

    Re: Create Array Timer

    I get and error Get syntax error, End Property must be preceded by matching property

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Create Array Timer

    Sorry. That's what I get for not using an IDE.
    vb.net Code:
    1. Public Class SomeClass
    2.  
    3.     Private _timers As New List(Of Timer)
    4.  
    5.     Public ReadOnly Property Timers() As List(Of Timer)
    6.         Get
    7.             Return _timers
    8.         End Get
    9.     End Property
    10.  
    11. End Class
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    PowerPoster Evil_Giraffe's Avatar
    Join Date
    Aug 2002
    Location
    Suffolk, UK
    Posts
    2,555

    Re: Create Array Timer

    It's a good practice to expose the collection as an IList, rather than just a List:

    vbnet Code:
    1. Public Class SomeClass
    2.  
    3.     Private _timers As New List(Of Timer)
    4.  
    5.     Public ReadOnly Property Timers() As IList(Of Timer)
    6.         Get
    7.             Return _timers
    8.         End Get
    9.     End Property
    10.  
    11. 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.

  7. #7
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    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.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    199

    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

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    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?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jan 2007
    Posts
    199

    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
  •  



Click Here to Expand Forum to Full Width