Results 1 to 2 of 2

Thread: Dynamic Timer in a Dynamic Form.

  1. #1

    Thread Starter
    Lively Member manbearpig001's Avatar
    Join Date
    Oct 2009
    Posts
    73

    Dynamic Timer in a Dynamic Form.

    Hey guys. I've been having trouble with this and need another angle. Ive managed to dynamically create a form, and dynamically create a timer, but i have not been able to create the timer on the dynamic form. Specifically, i need to be able to have the timer itself create another form with a timer. (I realize that this would create a new form every interval on the timer, that is what i want to do)

    I need help with these things;

    1. A way to add the timer to the dynamic form, and maintain the timer sub on my main form.

    2. A way to create the whole thing over (dynamic form and timer) through the previous dynamic form and timer.

    I was thinking i could use a collection/array to store the forms and timers, but i'm still having trouble figuring out how to add entire forms or timers into a list. (A timer is not considered a control, so i cant use a controlcollection...)

    Heres my code;

    Code:
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            Dim frm As New Form
            Dim timer As New System.Timers.Timer(3000)
            Dim tcollection As Collection
            tcollection.Add(timer)
            Dim i As Integer = 1
            frm.Width = 500
            frm.Height = 500
            frm.BackColor = Color.Black
            frm.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
            timer.Enabled = True
            AddHandler timer.Elapsed, AddressOf Timer_tick
        End Sub
        Private Sub Timer_tick(ByVal sender As System.Object, ByVal e As System.Timers.ElapsedEventArgs)
            MsgBox("test")
        End Sub

    *EDIT* Btw, my idea was to use i as a variable that increases every time a form is created, then insert the form into the array, with i as the integer. i just need to know how to create a new form with a different name each time. (as with timers)
    Last edited by manbearpig001; Feb 20th, 2010 at 01:28 AM.

  2. #2
    VB Addict Pradeep1210's Avatar
    Join Date
    Apr 2004
    Location
    Inside the CPU...
    Posts
    6,614

    Re: Dynamic Timer in a Dynamic Form.

    Try like this:

    vb.net Code:
    1. Public Class Form1
    2.  
    3.     Private MyForms As New List(Of Form)
    4.  
    5.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    6.         CreateForm()
    7.     End Sub
    8.  
    9.     Private Sub CreateForm()
    10.         Dim frm As New Form
    11.         frm.Width = 500
    12.         frm.Height = 500
    13.         frm.BackColor = Color.Black
    14.         frm.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
    15.         frm.Name = "Form" & MyForms.Count + 1
    16.  
    17.         Dim components As New System.ComponentModel.Container
    18.         Dim timer As New System.Windows.Forms.Timer(components)
    19.         timer.Interval = 3000
    20.         timer.Enabled = True
    21.         AddHandler timer.Tick, AddressOf Timer_Tick
    22.         frm.Show()
    23.  
    24.         MyForms.Add(frm)
    25.         Debug.Print("Form Created: " & frm.Name)
    26.     End Sub
    27.  
    28.     Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
    29.         CreateForm()
    30.     End Sub
    31.  
    32. End Class
    Pradeep, Microsoft MVP (Visual Basic)
    Please appreciate posts that have helped you by clicking icon on the left of the post.
    "A problem well stated is a problem half solved." — Charles F. Kettering

    Read articles on My Blog101 LINQ SamplesJSON ValidatorXML Schema Validator"How Do I" videos on MSDNVB.NET and C# ComparisonGood Coding PracticesVBForums Reputation SaverString EnumSuper Simple Tetris Game


    (2010-2013)
    NB: I do not answer coding questions via PM. If you want my help, then make a post and PM me it's link. If I can help, trust me I will...

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