0

Following is a custom control called JobTimer created by inheriting/extending the standard control Timer. Purpose: In order to get a custom property named JobID added to the control.

public class JobTimer : System.Windows.Forms.Timer
{
    private int IntJobID;
    public int JobID
    {
        get{return IntJobID;}
        set{IntJobID = value;}
    }
}

These controls are getting created programmatically during run-time. Following is the code that does this:

public static void CreateTimer(int JobID) 
{
    JobTimer ControlJobTimer = new JobTimer();
    ControlJobTimer.Name = "JobTimer" + JobID.ToString(); //Error occurs here. 
    ControlJobTimer.Enabled = true;
    ControlJobTimer.JobID = JobID;
    ControlJobTimer.Interval = 30000;
    ControlJobTimer.Tick += new EventHandler(JobTimer_Tick);
    ControlJobTimer.Start(); 
}

Problem I am facing here, is that I am not able to "set" the standard property Timer.Name for this control.

ERROR: NameSpace.JobTimer does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type NameSpace.JobTimer could be found (are you missing a using directive or an assembly reference?) ![enter image description here][1]

Even if the following line of code is supposed to set the name for this control as "ControlJobTimer"

JobTimer ControlJobTimer = new JobTimer(); 

Shouldn't I still be able to "Rename" a control at run-time? To my knowledge, this is doable with standard controls and should be a possible option with custom controls as well.

My requirement is that I want to be able to set a naming convention for the timer controls created at run-time. Please look into this and let me know what I am missing and if there is a workaround to achieve this function.

John Saunders
  • 160,644
  • 26
  • 247
  • 397
prgSRR
  • 169
  • 4
  • 11
  • What error are you getting? (Not sure enabling it first is a good idea either) – Sayse Sep 11 '14 at 07:55
  • That is the error that occurs there? – Rowland Shaw Sep 11 '14 at 07:55
  • @Sayse: I have added the error message. Also placed the enabling line of code next to the naming line of code. – prgSRR Sep 11 '14 at 08:19
  • prgSRR - Looking at [the documentation](http://msdn.microsoft.com/en-us/library/system.windows.forms.timer(v=vs.110).aspx), Timer isn't a control which is why it is erroring, It in fact, does *not* have a name property – Sayse Sep 11 '14 at 08:22
  • @Sayse: Thanks for the finding. What is puzzling though, is that these controls (both the standard Timer control and the custom JobTimer control) have a Name property when dragged onto a form in design-time. Any insights on this? – prgSRR Sep 11 '14 at 09:09
  • What is the use of *renaming* control at run-time? If you give control a name at design time, then it is accessible by that name in code and can be compiled. But why would you want to name `label1` differently? If you think to use name to reference specific timer, then simply have storage (`Dictionary<>`), using form `components` is not the best storage, I'd say it's purely for form autogenerated code (to dispose all components). You can implement disposing of your timers explicitly, on form closing or application exit. – Sinatr Sep 11 '14 at 09:10
  • @Sinatr: Yes, I was initially hoping to reference the timers with the name but then I came up with the custom property "JobID" for that purpose. So it is just a matter of naming convention or standards at this time. As for the disposing of these controls, it seems to happen automatically when application stops, so that is not an issue. – prgSRR Sep 11 '14 at 09:16

1 Answers1

0

Whilst the Windows Forms designer shows a (Name) in the Proprties window, the control itself doesn't have a Name property - The designer just uses this to name the member variable that holds the reference to the control.

There's nothing to stop you adding a Name property though, although, as you say, you already have the JobId so that code can tell which timer it is.

Rowland Shaw
  • 37,700
  • 14
  • 97
  • 166