473,321 Members | 1,877 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,321 software developers and data experts.

C# WinApp : Iterate through Timers in a form

Shashi Sadasivan
1,435 Expert 1GB
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
9 2853
radcaesar
759 Expert 512MB
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
1,435 Expert 1GB
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
9,735 Expert Mod 8TB
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
7,872 Expert 4TB
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
1,435 Expert 1GB
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
7,872 Expert 4TB
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
1,435 Expert 1GB
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
7,872 Expert 4TB
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
1,435 Expert 1GB
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

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

Similar topics

3
by: Nathan Kovac | last post by:
I have a feeling I am missing something simple, but I just can't find it. Perhaps someone can give me a lead on where to look. I will describe the issue then post my code to the web service. My...
10
by: WhiteSocksGuy | last post by:
Help! I am new to Visual Basic .Net (version 2002) and I am trying to get a System.Timers.Timer to work for me to display a splash screen for about two seconds and then load the main form. I have...
5
by: Michael C# | last post by:
Hi all, I set up a System.Timers.Time in my app. The code basically just updates the screen, but since the processing performed is so CPU-intensive, I wanted to make sure it gets updated...
9
by: N! Xau | last post by:
hi I need a way to threat a certain number of timers in a homogeneous way. Let's say I have this code: select X case 1 timer1.enabled = true case 2
8
by: Jerry Spence1 | last post by:
I am trying to create timers on demand by doing the following: Dim NewTimer As New System.Timers.Timer NewTimer.Interval = 10000 AddHandler NewTimer.Elapsed, AddressOf Timeup NewTimer.Enabled =...
3
by: Maarten | last post by:
Hi In my aplication it seems that my timer slows down everything, is there a better and/or diferent way than using a timer Maarten
4
by: Dave | last post by:
MS Virtual Server 2005 (not R2) with .Net 2003 I created a Console App and A windows form both with Timers in it. For the console i used system.timers and the windows form i used...
4
by: Liverpool fan | last post by:
I have a windows application written using VB .NET that encompasses a countdown timer modal dialog. The timer is a System.Timers.Timer with an interval of 1 second. AutoReset is not set so accepts...
0
nev
by: nev | last post by:
I know when is best to use some templates but listed below are most that I don't know when is best to use or even know how to use: I am creating a windows application with mysql backend. 1....
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.