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)
Re: Dynamic Timer in a Dynamic Form.
Try like this:
vb.net Code:
Public Class Form1
Private MyForms As New List(Of Form)
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
CreateForm()
End Sub
Private Sub CreateForm()
Dim frm As New Form
frm.Width = 500
frm.Height = 500
frm.BackColor = Color.Black
frm.FormBorderStyle = Windows.Forms.FormBorderStyle.Sizable
frm.Name = "Form" & MyForms.Count + 1
Dim components As New System.ComponentModel.Container
Dim timer As New System.Windows.Forms.Timer(components)
timer.Interval = 3000
timer.Enabled = True
AddHandler timer.Tick, AddressOf Timer_Tick
frm.Show()
MyForms.Add(frm)
Debug.Print("Form Created: " & frm.Name)
End Sub
Private Sub Timer_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs)
CreateForm()
End Sub
End Class