How to change color of form after every 3 sec. in vc#

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • information99
    New Member
    • Sep 2010
    • 4

    How to change color of form after every 3 sec. in vc#

    i want to change color of form after every 3 sec. in vc#. How it can be done with the help of timer. Do i need to use a loop if yes then how?

    Thanx
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    Add a Timer control to the form and implement code that handles time Timer's Tick event. In that method set the Window.Form.Bac kColor property.

    -Frinny

    Comment

    • information99
      New Member
      • Sep 2010
      • 4

      #3
      I have already tried

      I have already tried this but can u suggest me something else my code is
      Code:
       private void timer1_Tick(object sender, EventArgs e)
       {
         this.BackColor = Color.Red;
       }
      Plz. help me out

      Thankx
      Last edited by Frinavale; Sep 15 '10, 06:24 PM. Reason: Please post code in [code] ... [/code] tags. Added code tags.

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Try calling the Update method afterwords to force the control to refresh.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          That code always sets the background to red. Then what?

          Comment

          • Subin Ninan
            New Member
            • Sep 2010
            • 91

            #6
            Use an IF...ELSE condition in timer_tick event to set different colors depending upon value property of timer control.

            Comment

            • information99
              New Member
              • Sep 2010
              • 4

              #7
              if .. else not working

              hi
              thankx for ur suggestions. Somebody suggested me to use random class/method. but i dont know how to use here. plz. guide.

              Comment

              • Subin Ninan
                New Member
                • Sep 2010
                • 91

                #8
                you can use random class as:

                Code:
                Random rndObj = new Random();           
                if(rndObj.Next(1, 4).ToString()== "1")
                {
                this.BackColor = Color.Red;
                }
                else
                if(rndObj.Next(1, 4).ToString()== "2")
                {
                this.BackColor = Color.Green;
                }
                else
                if(rndObj.Next(1, 4).ToString()== "3")
                {
                this.BackColor = Color.Yellow;
                }
                else
                {
                this.BackColor = Color.White;
                }
                Last edited by Niheel; Sep 19 '10, 04:18 AM. Reason: added code tags to display code

                Comment

                • information99
                  New Member
                  • Sep 2010
                  • 4

                  #9
                  want to use random with timer

                  Thanks, but i want to change the color with timer.

                  Comment

                  • alzcuty
                    New Member
                    • Oct 2011
                    • 1

                    #10
                    just put a timer on the form and set its interval to 1000 that is, representing milliseconds... .

                    play Button:
                    Code:
                    Timer1.start();
                    Now on ur .cs file...

                    Code:
                    public int a=10;
                    then on the timer's tick event.put the following code
                    Code:
                    a--;
                    if(a==10)
                      this.backcolor= color.red;
                    
                    else if(a==9)
                      this.backcolor= color.blue;
                    and so on....
                    Last edited by Frinavale; Oct 5 '11, 03:26 PM. Reason: Added code tags.

                    Comment

                    Working...