473,836 Members | 1,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# WinApp : Iterate through Timers in a form

Shashi Sadasivan
1,435 Recognized Expert Top Contributor
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.T imer 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 2905
radcaesar
759 Recognized Expert Contributor
If i got you right, Why can't you access that control using FormName.TimerN ame to change its properties ?
Feb 5 '08 #2
Shashi Sadasivan
1,435 Recognized Expert Top Contributor
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 Recognized Expert Moderator Expert
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 Recognized Expert Expert
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 Recognized Expert Top Contributor
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 Recognized Expert Expert
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 Recognized Expert Top Contributor
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 Recognized Expert Expert
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 Recognized Expert Top Contributor
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
2202
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 issue is simply getting timers to work. I have a DatabaseManager.DataManagerFacade which contains a timer. Every 6 seconds it updates stock data using an online webservice. I wrote have 2 possible startup projects to make the service work. One...
10
2716
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 two forms (frmSplash and frmMain) and a code Module setup as my startup object. Here is the code that I have, when I try to run this part of the splash screen form shows and then program terminates. What am I doing wrong? Thanks,
5
9893
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 regularly; like every 1.5 secs. or so. I only ran into one issue - the MyTimer_Elapsed event handler was not updating the screen correctly all the time, often leaving large chunks of the screen un-painted for several seconds. On a whim I decided to...
9
1365
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
3175
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 = True NewTimer.Start() My problem then happens in the TimeUp routine
3
354
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
1415
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 system.windows.forms.timers. Now for the problem, with both Apps the timers will stop for no reason. I mean the logic in the timer events is simply timer.enable = false, counter+=1, timer.enable=true then end sub. The only thing that i have noticed is that...
4
11141
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 the default of True. The Elapsed event handler updates the dialog box with how long before it will close, acting as a timer itself. The dialog has a time to close property which is checked every time the Elapsed event fires. The problem I have...
0
1483
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. Windows Form - I use this mostly to create my windows forms 2. Dialog - I use this for windows that need user intervention 3. Explorer Form - maybe I will use this if I plan to make something like windows explorer 4. MDI Parent Form - I also use this...
0
9825
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10854
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10600
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9387
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7794
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6981
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5829
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4459
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.