By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,652 Members | 1,487 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,652 IT Pros & Developers. It's quick & easy.

C# WinApp : Iterate through Timers in a form

Shashi Sadasivan
Expert 100+
P: 1,435
In my current application, I have to set cettain defaults to controls that are displayed or are used.

so i have a class to which i send the form as a control, and iterate through each of its controls and child controls

However, I have to also edit the Interval property of any timers present in the form.

Timers come under the Windows.Forms.Timer namespace and i am currently iterating through all the Controls in the form.

How would I go about finding Timers in a form?

Expand|Select|Wrap|Line Numbers
  1. foreach (Control c in control.Controls)
  2.             {
  3.                 this.applyControlValues(c);
  4.                 if (c.HasChildren == true)
  5.                     this.applyKeyStrokes(c);
  6.             }
within applyKeyStrokes i check if the control c is of type text edit, or dropDown list, or gridview, etc

Thankyou
Feb 4 '08 #1
Share this Question
Share on Google+
9 Replies


radcaesar
Expert 100+
P: 759
If i got you right, Why can't you access that control using FormName.TimerName to change its properties ?
Feb 5 '08 #2

Shashi Sadasivan
Expert 100+
P: 1,435
Every form has to have a certain look and feel property.

Yes its funny that i change this on runtime (and do not creqate a static compiled method, but all becasue the client require to change the look and feel as they want)

So if i have 5 forms
and i pass the forms to a class which scans thruogh all the controls contained inside it using Fom.Controls
So whenever i create a new form, to apply the look and feel, all i do is pass the form object and the values and properties are set accordingly.

So if i have 5 forms, and all 5 forms have timers (could be off different names, and some forms may have more than 1 timer) then in this case i would like to iterate through all the timers inside the form !!!

Hope this is not a bad way to implement it.

Wouldnt mind other opinions!
Feb 5 '08 #3

Frinavale
Expert Mod 5K+
P: 9,731
Every form has to have a certain look and feel property.

Yes its funny that i change this on runtime (and do not creqate a static compiled method, but all becasue the client require to change the look and feel as they want)

So if i have 5 forms
and i pass the forms to a class which scans thruogh all the controls contained inside it using Fom.Controls
So whenever i create a new form, to apply the look and feel, all i do is pass the form object and the values and properties are set accordingly.

So if i have 5 forms, and all 5 forms have timers (could be off different names, and some forms may have more than 1 timer) then in this case i would like to iterate through all the timers inside the form !!!

Hope this is not a bad way to implement it.

Wouldnt mind other opinions!
I think you should consider using Properties to pass the controls you are wanting to deal with. This will save you the time and resource of looping through and looking for them.

-Frinny
Feb 5 '08 #4

Plater
Expert 5K+
P: 7,872
For every Control C that you get, if they are really a derrived class, you can discover what it is. Just the same way as if you were checking if they were a TextBox or anything?

Consider for example:
Expand|Select|Wrap|Line Numbers
  1. //assume c is the current control to examine
  2. if (c.GetType()==typeof(System.Windows.Forms.Timer)
  3. {//the control is a timer, cast it as a Timer and set it's properties
  4. }
  5.  
Feb 5 '08 #5

Shashi Sadasivan
Expert 100+
P: 1,435
For every Control C that you get, if they are really a derrived class, you can discover what it is. Just the same way as if you were checking if they were a TextBox or anything?

Consider for example:
Expand|Select|Wrap|Line Numbers
  1. //assume c is the current control to examine
  2. if (c.GetType()==typeof(System.Windows.Forms.Timer)
  3. {//the control is a timer, cast it as a Timer and set it's properties
  4. }
  5.  
HI plater
Actaully I did try that code...
It never enters into that condition.

I even tried
Timer t = (Timer)c;
but this gives a compile time error that I cannot directly cast a control to a Timer.

For the moment, I am implementing the timer properties from each page, and any changes means that I have to repeat the process (hopefully without missing any forms with timers in it)

The timers are controlling a progress bar to refresh the data on the screen.
Feb 5 '08 #6

Plater
Expert 5K+
P: 7,872
Ah ha!
It's because Timer doesn't inhereit from Control.
When you had a Timer to a Form, it doesn't get put in it's controls.

They take in a reference to the container and can be found with this:

Expand|Select|Wrap|Line Numbers
  1. foreach (IComponent ic in this.components.Components)
  2. {
  3.    if (ic.GetType() == typeof(System.Windows.Forms.Timer))
  4.    {//the control is a timer, cast it as a Timer and set it's properties
  5.    }
  6. }
  7.  
Feb 5 '08 #7

Shashi Sadasivan
Expert 100+
P: 1,435
Ah ha!
It's because Timer doesn't inhereit from Control.
When you had a Timer to a Form, it doesn't get put in it's controls.

They take in a reference to the container and can be found with this:

Expand|Select|Wrap|Line Numbers
  1. foreach (IComponent ic in this.components.Components)
  2. {
  3.    if (ic.GetType() == typeof(System.Windows.Forms.Timer))
  4.    {//the control is a timer, cast it as a Timer and set it's properties
  5.    }
  6. }
  7.  
Yep that worked,
I realised it didnt inherit from a control.
However, that code which you provided, can only be placed in the form where the controls reside.
What I do is pass the form class to another class, which iterates through all controls and sets their default values. However when I send the form to this class, I cannot access the components object, maybe because it is protected.
Would I have to change any properties of the form?

Thanks a lot
Feb 5 '08 #8

Plater
Expert 5K+
P: 7,872
See I found that funny to. Forms have a public property called "Container" that also has an IComponent Collection, but it always claims to be null.

Can you pass the form and a reference to it's protected container to your function? (Not the greatest, but it might work?
Feb 6 '08 #9

Shashi Sadasivan
Expert 100+
P: 1,435
See I found that funny to. Forms have a public property called "Container" that also has an IComponent Collection, but it always claims to be null.

Can you pass the form and a reference to it's protected container to your function? (Not the greatest, but it might work?
Yes the Form.Container always sits at null.
For the moment i have a zillion forms, and only a few with timers in it.
For the moment I will look at setting the timer property within every But will create an overloaded method with passing the forms protected container !

Cheers, thanks a lot
Feb 6 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.