Click here to Skip to main content
15,911,360 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I would like to iterate through all the controls on a form and store/retrieve the .Interval values for all the Timers found.

I thought I could use the following code but it does not find my timer.

C#
foreach (Control ctrl in this.Controls)
{
    if (ctrl.GetType() == typeof(Timer))
    {
        MessageBox.Show("Found 1");

    }
}


Where would I find these timers if not in this.Controls? I would hope I would not have to dynamically add them at run time in order to have a collection of them.
Posted

If this is Winforms, and you have added your timer with the designer, then you will find it in the yourForm.components private member variable, rather than in Controls property.

Have a look at YourFile.Designer.cs file, you will find in there how your timer is incorporated in your form.

Hope this helps.
 
Share this answer
 
Comments
David J Perez 4-Nov-11 18:21pm    
Thanks, that's exactly what I needed. Not sure what's going on back there in xxx.Designer.cs but if I want to find non-timer controls I will use this.Controls and if I don't find it there I will try this.components.Components. Very intuitive :)

Thanks again!
phil.o 4-Nov-11 18:27pm    
Not sure what's going on back there in xxx.Designer.cs

There you can find the declaration of all that you add to your form with the designer (controls, components). For each element that you drag onto your form, a private variable is declared in the designer file. And it also contains several methods like Dispose() and InitializeComponent(). In this last one, properties of controls and components are set, collections are filled, and events are wired. It is plain C#, so no special magic under.
David J Perez 4-Nov-11 18:35pm    
I guess what I am referring to is the fact that there is not a single collection that has all of them. I am coming from the VB6 world where if I wanted to find any control on my form I would check Me.Controls collection and everything was in there, timers and non-timers. Looks like some kind of kludge to have a Components collection along side the Controls collection. And to have it in a section of code that I don't have control over makes it even more suspect.

Plus, right now I am unable to get Components[i].Name because the .Name property does not exist for the Timer or the Component object.
Did you try
C#
for (int i = 0; i < this.Controls.Count; i++)
            {
                if (this.Controls[i].Name.Contains("timer"))
                {
                    MessageBox.Show("Found it's name is ->"+this.Controls[i].Name);
                }
            }
 
Share this answer
 
Comments
David J Perez 4-Nov-11 18:08pm    
Just tried it and got the same result. I do not believe Timers are in the Controls collection because Timers do not inherit from the Controls class. But I am not sure where to get a collection of them.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900