C# WinApp : Iterate through Timers in a form

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Shashi Sadasivan
    Recognized Expert Top Contributor
    • Aug 2007
    • 1435

    C# WinApp : Iterate through Timers in a form

    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?

    [CODE=cpp]foreach (Control c in control.Control s)
    {
    this.applyContr olValues(c);
    if (c.HasChildren == true)
    this.applyKeySt rokes(c);
    }[/CODE]

    within applyKeyStrokes i check if the control c is of type text edit, or dropDown list, or gridview, etc

    Thankyou
  • radcaesar
    Recognized Expert Contributor
    • Sep 2006
    • 759

    #2
    If i got you right, Why can't you access that control using FormName.TimerN ame to change its properties ?

    Comment

    • Shashi Sadasivan
      Recognized Expert Top Contributor
      • Aug 2007
      • 1435

      #3
      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!

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Originally posted by Shashi Sadasivan
        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

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          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:
          Code:
          //assume c is the current control to examine
          if (c.GetType()==typeof(System.Windows.Forms.Timer)
          {//the control is a timer, cast it as a Timer and set it's properties
          }

          Comment

          • Shashi Sadasivan
            Recognized Expert Top Contributor
            • Aug 2007
            • 1435

            #6
            Originally posted by Plater
            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:
            Code:
            //assume c is the current control to examine
            if (c.GetType()==typeof(System.Windows.Forms.Timer)
            {//the control is a timer, cast it as a Timer and set it's properties
            }
            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.

            Comment

            • Plater
              Recognized Expert Expert
              • Apr 2007
              • 7872

              #7
              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:

              Code:
              foreach (IComponent ic in this.components.Components)
              {
                 if (ic.GetType() == typeof(System.Windows.Forms.Timer))
                 {//the control is a timer, cast it as a Timer and set it's properties
                 }
              }

              Comment

              • Shashi Sadasivan
                Recognized Expert Top Contributor
                • Aug 2007
                • 1435

                #8
                Originally posted by Plater
                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:

                Code:
                foreach (IComponent ic in this.components.Components)
                {
                   if (ic.GetType() == typeof(System.Windows.Forms.Timer))
                   {//the control is a timer, cast it as a Timer and set it's properties
                   }
                }
                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

                Comment

                • Plater
                  Recognized Expert Expert
                  • Apr 2007
                  • 7872

                  #9
                  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?

                  Comment

                  • Shashi Sadasivan
                    Recognized Expert Top Contributor
                    • Aug 2007
                    • 1435

                    #10
                    Originally posted by Plater
                    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

                    Comment

                    Working...