Can not set the name property of a timer at runtime - VB.NET
-
Hi there. Sorry if this is long winded. I am relatively new to programming and have managed so far with google and a lot of patience! I am having a problem naming timer contols at runtime. This is what I am trying to achieve. 1. I connect to my dataset and to my first record 2. I create a loop for the recordset 3. I create various objects on a form and name them using the info in the dataset I also need to create timers and name them using the first field of my dataset. It needs to be dynamic as its database driven. There could be as many as 100 timers at any one point. I can use the Button.name = "myname" fine for most controls but doing a Timer.name does not work This is some of my code (some variable values not shown): 'Counter to move through the database Dataset Dim RecordsCount As Integer RecordsCount = ManagerDataSet.Robots.Count 'Declare name incremented with record number to make it unique Dim btnSSName(RecordsCount) As Button For myLoop = 0 To (RecordsCount - 1) 'Create a button btnSSName(myLoop) = New Button() btnSSName(myLoop).Location = New Drawing.Point(btnSSPosVer, lblposHoz) 'lblposVer, ProgBarPosHoz) btnSSName(myLoop).Name = "btnSS" + currentRobot btnSSName(myLoop).Text = currentRobot btnSSName(myLoop).Width = 70 btnSSName(myLoop).Height = 23 btnSSName(myLoop).BackColor = Color.Red btnSSName(myLoop).ForeColor = Color.White TabControl1.TabPages(0).Controls.Add(btnSSName(myLoop)) next myLoop That all works great for populating objects on the form. When I create a timer in a similar way: Dim tmr(RecordsCount) As Timer tmr(myLoop) = New Timer tmr(myLoop).Interval = whatevermydatasetrecordis tmr(myLoop).Name = "myuniquename" AddHandler tmr(myLoop).Tick, AddressOf hndlifespan *****The Problem ****** Using . . tmr(myLoop).Name = "myuniquename" I get a error of 'Name' is not a member of 'System.Windows.Forms.Timer' When one of my dynamicly created buttons is pressed it goes to the dynamically created button click handler. This in turn knows which button was pressed by asking its name. At this point I want to launch a specific timer based on the button name. So if the button "Alpha" was pressed I want to enable the "Alpha" timer (that was created dynamically from the dataset with its associated interval). I have created a timer at design time which does this: Friend WithEvents billy A
-
Hi there. Sorry if this is long winded. I am relatively new to programming and have managed so far with google and a lot of patience! I am having a problem naming timer contols at runtime. This is what I am trying to achieve. 1. I connect to my dataset and to my first record 2. I create a loop for the recordset 3. I create various objects on a form and name them using the info in the dataset I also need to create timers and name them using the first field of my dataset. It needs to be dynamic as its database driven. There could be as many as 100 timers at any one point. I can use the Button.name = "myname" fine for most controls but doing a Timer.name does not work This is some of my code (some variable values not shown): 'Counter to move through the database Dataset Dim RecordsCount As Integer RecordsCount = ManagerDataSet.Robots.Count 'Declare name incremented with record number to make it unique Dim btnSSName(RecordsCount) As Button For myLoop = 0 To (RecordsCount - 1) 'Create a button btnSSName(myLoop) = New Button() btnSSName(myLoop).Location = New Drawing.Point(btnSSPosVer, lblposHoz) 'lblposVer, ProgBarPosHoz) btnSSName(myLoop).Name = "btnSS" + currentRobot btnSSName(myLoop).Text = currentRobot btnSSName(myLoop).Width = 70 btnSSName(myLoop).Height = 23 btnSSName(myLoop).BackColor = Color.Red btnSSName(myLoop).ForeColor = Color.White TabControl1.TabPages(0).Controls.Add(btnSSName(myLoop)) next myLoop That all works great for populating objects on the form. When I create a timer in a similar way: Dim tmr(RecordsCount) As Timer tmr(myLoop) = New Timer tmr(myLoop).Interval = whatevermydatasetrecordis tmr(myLoop).Name = "myuniquename" AddHandler tmr(myLoop).Tick, AddressOf hndlifespan *****The Problem ****** Using . . tmr(myLoop).Name = "myuniquename" I get a error of 'Name' is not a member of 'System.Windows.Forms.Timer' When one of my dynamicly created buttons is pressed it goes to the dynamically created button click handler. This in turn knows which button was pressed by asking its name. At this point I want to launch a specific timer based on the button name. So if the button "Alpha" was pressed I want to enable the "Alpha" timer (that was created dynamically from the dataset with its associated interval). I have created a timer at design time which does this: Friend WithEvents billy A
-
Hi there. Sorry if this is long winded. I am relatively new to programming and have managed so far with google and a lot of patience! I am having a problem naming timer contols at runtime. This is what I am trying to achieve. 1. I connect to my dataset and to my first record 2. I create a loop for the recordset 3. I create various objects on a form and name them using the info in the dataset I also need to create timers and name them using the first field of my dataset. It needs to be dynamic as its database driven. There could be as many as 100 timers at any one point. I can use the Button.name = "myname" fine for most controls but doing a Timer.name does not work This is some of my code (some variable values not shown): 'Counter to move through the database Dataset Dim RecordsCount As Integer RecordsCount = ManagerDataSet.Robots.Count 'Declare name incremented with record number to make it unique Dim btnSSName(RecordsCount) As Button For myLoop = 0 To (RecordsCount - 1) 'Create a button btnSSName(myLoop) = New Button() btnSSName(myLoop).Location = New Drawing.Point(btnSSPosVer, lblposHoz) 'lblposVer, ProgBarPosHoz) btnSSName(myLoop).Name = "btnSS" + currentRobot btnSSName(myLoop).Text = currentRobot btnSSName(myLoop).Width = 70 btnSSName(myLoop).Height = 23 btnSSName(myLoop).BackColor = Color.Red btnSSName(myLoop).ForeColor = Color.White TabControl1.TabPages(0).Controls.Add(btnSSName(myLoop)) next myLoop That all works great for populating objects on the form. When I create a timer in a similar way: Dim tmr(RecordsCount) As Timer tmr(myLoop) = New Timer tmr(myLoop).Interval = whatevermydatasetrecordis tmr(myLoop).Name = "myuniquename" AddHandler tmr(myLoop).Tick, AddressOf hndlifespan *****The Problem ****** Using . . tmr(myLoop).Name = "myuniquename" I get a error of 'Name' is not a member of 'System.Windows.Forms.Timer' When one of my dynamicly created buttons is pressed it goes to the dynamically created button click handler. This in turn knows which button was pressed by asking its name. At this point I want to launch a specific timer based on the button name. So if the button "Alpha" was pressed I want to enable the "Alpha" timer (that was created dynamically from the dataset with its associated interval). I have created a timer at design time which does this: Friend WithEvents billy A
Hi, seems you are confused: - every Control has a Name property - it gets used by Visual Designer to link back to the variable that will hold the Control's reference - a Timer is NOT a Control, it is a Component - Components don't have a Name property - the Name of a Control does not relate to the name of the variable holding a reference to that Control, unless you set them identically, as Visual Designer does. So you will need another scheme to achieve whatever it is you are trying to achieve. And please show properly formatted code, i.e. use PRE tags. :)
Luc Pattyn [Forum Guidelines] [Why QA sucks] [My Articles]
I only read code that is properly formatted, adding PRE tags is the easiest way to obtain that.
[The QA section does it automatically now, I hope we soon get it on regular forums as well]
-
Hi there. Sorry if this is long winded. I am relatively new to programming and have managed so far with google and a lot of patience! I am having a problem naming timer contols at runtime. This is what I am trying to achieve. 1. I connect to my dataset and to my first record 2. I create a loop for the recordset 3. I create various objects on a form and name them using the info in the dataset I also need to create timers and name them using the first field of my dataset. It needs to be dynamic as its database driven. There could be as many as 100 timers at any one point. I can use the Button.name = "myname" fine for most controls but doing a Timer.name does not work This is some of my code (some variable values not shown): 'Counter to move through the database Dataset Dim RecordsCount As Integer RecordsCount = ManagerDataSet.Robots.Count 'Declare name incremented with record number to make it unique Dim btnSSName(RecordsCount) As Button For myLoop = 0 To (RecordsCount - 1) 'Create a button btnSSName(myLoop) = New Button() btnSSName(myLoop).Location = New Drawing.Point(btnSSPosVer, lblposHoz) 'lblposVer, ProgBarPosHoz) btnSSName(myLoop).Name = "btnSS" + currentRobot btnSSName(myLoop).Text = currentRobot btnSSName(myLoop).Width = 70 btnSSName(myLoop).Height = 23 btnSSName(myLoop).BackColor = Color.Red btnSSName(myLoop).ForeColor = Color.White TabControl1.TabPages(0).Controls.Add(btnSSName(myLoop)) next myLoop That all works great for populating objects on the form. When I create a timer in a similar way: Dim tmr(RecordsCount) As Timer tmr(myLoop) = New Timer tmr(myLoop).Interval = whatevermydatasetrecordis tmr(myLoop).Name = "myuniquename" AddHandler tmr(myLoop).Tick, AddressOf hndlifespan *****The Problem ****** Using . . tmr(myLoop).Name = "myuniquename" I get a error of 'Name' is not a member of 'System.Windows.Forms.Timer' When one of my dynamicly created buttons is pressed it goes to the dynamically created button click handler. This in turn knows which button was pressed by asking its name. At this point I want to launch a specific timer based on the button name. So if the button "Alpha" was pressed I want to enable the "Alpha" timer (that was created dynamically from the dataset with its associated interval). I have created a timer at design time which does this: Friend WithEvents billy A
When pasting code use the code block for chunks of code or inline code if using it within [
Dim xyz
] a sentence. It makes it a whole lot easier to read, and you will more likely get a response. How many timers are you trying to create? Are you wanting to change the name just so you can easily reference them dynamically?Dave Don't forget rate messages! Find Me On: Web|Facebook|Twitter|LinkedIn Waving? dave.m.auld[at]googlewave.com
-
Hi there. Sorry if this is long winded. I am relatively new to programming and have managed so far with google and a lot of patience! I am having a problem naming timer contols at runtime. This is what I am trying to achieve. 1. I connect to my dataset and to my first record 2. I create a loop for the recordset 3. I create various objects on a form and name them using the info in the dataset I also need to create timers and name them using the first field of my dataset. It needs to be dynamic as its database driven. There could be as many as 100 timers at any one point. I can use the Button.name = "myname" fine for most controls but doing a Timer.name does not work This is some of my code (some variable values not shown): 'Counter to move through the database Dataset Dim RecordsCount As Integer RecordsCount = ManagerDataSet.Robots.Count 'Declare name incremented with record number to make it unique Dim btnSSName(RecordsCount) As Button For myLoop = 0 To (RecordsCount - 1) 'Create a button btnSSName(myLoop) = New Button() btnSSName(myLoop).Location = New Drawing.Point(btnSSPosVer, lblposHoz) 'lblposVer, ProgBarPosHoz) btnSSName(myLoop).Name = "btnSS" + currentRobot btnSSName(myLoop).Text = currentRobot btnSSName(myLoop).Width = 70 btnSSName(myLoop).Height = 23 btnSSName(myLoop).BackColor = Color.Red btnSSName(myLoop).ForeColor = Color.White TabControl1.TabPages(0).Controls.Add(btnSSName(myLoop)) next myLoop That all works great for populating objects on the form. When I create a timer in a similar way: Dim tmr(RecordsCount) As Timer tmr(myLoop) = New Timer tmr(myLoop).Interval = whatevermydatasetrecordis tmr(myLoop).Name = "myuniquename" AddHandler tmr(myLoop).Tick, AddressOf hndlifespan *****The Problem ****** Using . . tmr(myLoop).Name = "myuniquename" I get a error of 'Name' is not a member of 'System.Windows.Forms.Timer' When one of my dynamicly created buttons is pressed it goes to the dynamically created button click handler. This in turn knows which button was pressed by asking its name. At this point I want to launch a specific timer based on the button name. So if the button "Alpha" was pressed I want to enable the "Alpha" timer (that was created dynamically from the dataset with its associated interval). I have created a timer at design time which does this: Friend WithEvents billy A
You are getting that error since Timer does not has Name property. If I had do this, I would have gone fro something like this (C# code): In the Load event (or wherever you want to create a button):
Button btn = new Button(); btn.Name = "SomeName1"; btn.Click += new EventHandler(btn\_Click); Button btn = new Button(); btn.Name = "SomeName2"; btn.Click += new EventHandler(btn\_Click);
.
.
.
And so oneNote that all the buttons have same click event. Now, in the click event:
Button btn = sender as Button; System.Windows.Forms.Timer timer = new System.Windows.Forms.Timer(); timer.Tag = btn.Name; timer.Tick += new EventHandler(timer\_Tick);
Here, you can place a check if that timer has already been created. Now, all the timers will have same tick event. So, in that event,
System.Windows.Forms.Timer timer = sender as System.Windows.Forms.Timer; switch(timer.Tag.ToString()){ case "buttonName": break; }
Using that switch case, you can identify the timer and button pair. If you can explain why are you doing all this, someone can show up with a better solution than this.
"No matter how many fish in the sea; it will be so empty without me." - From song "Without me" by Eminem
-
Hi there. Sorry if this is long winded. I am relatively new to programming and have managed so far with google and a lot of patience! I am having a problem naming timer contols at runtime. This is what I am trying to achieve. 1. I connect to my dataset and to my first record 2. I create a loop for the recordset 3. I create various objects on a form and name them using the info in the dataset I also need to create timers and name them using the first field of my dataset. It needs to be dynamic as its database driven. There could be as many as 100 timers at any one point. I can use the Button.name = "myname" fine for most controls but doing a Timer.name does not work This is some of my code (some variable values not shown): 'Counter to move through the database Dataset Dim RecordsCount As Integer RecordsCount = ManagerDataSet.Robots.Count 'Declare name incremented with record number to make it unique Dim btnSSName(RecordsCount) As Button For myLoop = 0 To (RecordsCount - 1) 'Create a button btnSSName(myLoop) = New Button() btnSSName(myLoop).Location = New Drawing.Point(btnSSPosVer, lblposHoz) 'lblposVer, ProgBarPosHoz) btnSSName(myLoop).Name = "btnSS" + currentRobot btnSSName(myLoop).Text = currentRobot btnSSName(myLoop).Width = 70 btnSSName(myLoop).Height = 23 btnSSName(myLoop).BackColor = Color.Red btnSSName(myLoop).ForeColor = Color.White TabControl1.TabPages(0).Controls.Add(btnSSName(myLoop)) next myLoop That all works great for populating objects on the form. When I create a timer in a similar way: Dim tmr(RecordsCount) As Timer tmr(myLoop) = New Timer tmr(myLoop).Interval = whatevermydatasetrecordis tmr(myLoop).Name = "myuniquename" AddHandler tmr(myLoop).Tick, AddressOf hndlifespan *****The Problem ****** Using . . tmr(myLoop).Name = "myuniquename" I get a error of 'Name' is not a member of 'System.Windows.Forms.Timer' When one of my dynamicly created buttons is pressed it goes to the dynamically created button click handler. This in turn knows which button was pressed by asking its name. At this point I want to launch a specific timer based on the button name. So if the button "Alpha" was pressed I want to enable the "Alpha" timer (that was created dynamically from the dataset with its associated interval). I have created a timer at design time which does this: Friend WithEvents billy A
Hi Paul If I am getting you correctly, you need to start a specific timer when it's corresponding button is clicked. To be honest I never followed your code, but I would do something like this
Public Class Form1
Private lstButtons As New List(Of Button) Private lstTimers As New List(Of Timer) 'create array of integers for timer intervals Private arrIntegers() As Integer = {25, 46, 91, 38, 50} Private Sub Form1\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load For i As Integer = 0 To 4 Dim newButton As New Button newButton.Name = i newButton.Text = "Button " & i newButton.Width = 75 newButton.Height = 25 newButton.Left = 10 newButton.Top = 10 + (25 \* i) + (5 \* i) newButton.Parent = Me newButton.Visible = True AddHandler newButton.Click, AddressOf ButtonsClick lstButtons.Add(newButton) Dim newTimer As New Timer newTimer.Interval = arrIntegers(i) AddHandler newTimer.Tick, AddressOf TimersTick lstTimers.Add(newTimer) Next End Sub Private Sub ButtonsClick(ByVal sender As Object, ByVal e As System.EventArgs) Dim intIndex As Integer = CInt(CType(sender, Button).Name) lstTimers(intIndex).Start() End Sub Private Sub TimersTick(ByVal sender As Object, ByVal e As System.EventArgs) CType(sender, Timer).Stop() MsgBox(CType(sender, Timer).ToString) End Sub
End Class
I have used lists rather than arrays (personal preference), but the gist of it should be the same. Instead of giving the buttons a name in the form of a string, use a number, and then reference the timer using the button name as an index. Hope this helps
-
Hi there. Sorry if this is long winded. I am relatively new to programming and have managed so far with google and a lot of patience! I am having a problem naming timer contols at runtime. This is what I am trying to achieve. 1. I connect to my dataset and to my first record 2. I create a loop for the recordset 3. I create various objects on a form and name them using the info in the dataset I also need to create timers and name them using the first field of my dataset. It needs to be dynamic as its database driven. There could be as many as 100 timers at any one point. I can use the Button.name = "myname" fine for most controls but doing a Timer.name does not work This is some of my code (some variable values not shown): 'Counter to move through the database Dataset Dim RecordsCount As Integer RecordsCount = ManagerDataSet.Robots.Count 'Declare name incremented with record number to make it unique Dim btnSSName(RecordsCount) As Button For myLoop = 0 To (RecordsCount - 1) 'Create a button btnSSName(myLoop) = New Button() btnSSName(myLoop).Location = New Drawing.Point(btnSSPosVer, lblposHoz) 'lblposVer, ProgBarPosHoz) btnSSName(myLoop).Name = "btnSS" + currentRobot btnSSName(myLoop).Text = currentRobot btnSSName(myLoop).Width = 70 btnSSName(myLoop).Height = 23 btnSSName(myLoop).BackColor = Color.Red btnSSName(myLoop).ForeColor = Color.White TabControl1.TabPages(0).Controls.Add(btnSSName(myLoop)) next myLoop That all works great for populating objects on the form. When I create a timer in a similar way: Dim tmr(RecordsCount) As Timer tmr(myLoop) = New Timer tmr(myLoop).Interval = whatevermydatasetrecordis tmr(myLoop).Name = "myuniquename" AddHandler tmr(myLoop).Tick, AddressOf hndlifespan *****The Problem ****** Using . . tmr(myLoop).Name = "myuniquename" I get a error of 'Name' is not a member of 'System.Windows.Forms.Timer' When one of my dynamicly created buttons is pressed it goes to the dynamically created button click handler. This in turn knows which button was pressed by asking its name. At this point I want to launch a specific timer based on the button name. So if the button "Alpha" was pressed I want to enable the "Alpha" timer (that was created dynamically from the dataset with its associated interval). I have created a timer at design time which does this: Friend WithEvents billy A
On top of what everyone else said, you have to be careful creating and disposing the System.Windows.Forms.Timer. Most people do not realize you have to call Dispose on them when you destroy them. Since Timers are not unlimited, you will eventually run the system dry on resources if you constantly create and destory Timers in your app.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009... -
On top of what everyone else said, you have to be careful creating and disposing the System.Windows.Forms.Timer. Most people do not realize you have to call Dispose on them when you destroy them. Since Timers are not unlimited, you will eventually run the system dry on resources if you constantly create and destory Timers in your app.
A guide to posting questions on CodeProject[^]
Dave Kreskowiak Microsoft MVP Visual Developer - Visual Basic
2006, 2007, 2008
But no longer in 2009...Thank you everyone I really appreciate it. I took some time out and realised I only need one timer event. There is still one thing I am struggling with to understand but I put it in a new thread as its a different question.
-
Thank you everyone I really appreciate it. I took some time out and realised I only need one timer event. There is still one thing I am struggling with to understand but I put it in a new thread as its a different question.
Im not quite sure I understand why it is you needed alot of timers, as im sure there would be a better way to do it. I would have thought that you could just use Threads.
Dim t As New Threading.Thread(AddressOf myThread)
Private Sub btnStart\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click t.Start() End Sub Private Sub myThread() Do 'Work to be done '.... ' Waits 5 seconds before next iteration Threading.Thread.Sleep(New TimeSpan(0, 0, 5)) 'Or for miliseconds use this: Threading.Thread.Sleep(500) Loop End Sub
The myThread method will continue running until either you run a t.stop() from somewhere else in your code or you call an "Exit Loop" from within the Do Loop. Please Note: If you are updating controls on a form from a threaded function you will need to use a Delegate, I can send you an example if you cannot find one. Also if you have a number of threads accessing the same resources you might need to use a Mutex to limit access violations. Hope this helps.
-
Im not quite sure I understand why it is you needed alot of timers, as im sure there would be a better way to do it. I would have thought that you could just use Threads.
Dim t As New Threading.Thread(AddressOf myThread)
Private Sub btnStart\_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click t.Start() End Sub Private Sub myThread() Do 'Work to be done '.... ' Waits 5 seconds before next iteration Threading.Thread.Sleep(New TimeSpan(0, 0, 5)) 'Or for miliseconds use this: Threading.Thread.Sleep(500) Loop End Sub
The myThread method will continue running until either you run a t.stop() from somewhere else in your code or you call an "Exit Loop" from within the Do Loop. Please Note: If you are updating controls on a form from a threaded function you will need to use a Delegate, I can send you an example if you cannot find one. Also if you have a number of threads accessing the same resources you might need to use a Mutex to limit access violations. Hope this helps.
Also might I add, from experience you might want to monitor how much memory your application uses after 1 hour of your software running then 2.... Just in case there are any memory leaks. Hope this helps