Search code examples
c#winformstimercustom-controlsprogrammatically-created

Custom Control lacking standard properties


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.


Solution

  • 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.