none
Creating a Variable to Stop an Already Existing Timer in C# RRS feed

  • Question

  • So, I'm Trying to write a function for the following lines of code so that I don't have to re-write it with different values every 

    time I have to use it.

    private void Timer1_Tick(object sender, EventArgs e)
            {
                if (Blue >= 180)
                {
                    timer1.Stop();
                }
                else
                {
                    Blue = Blue + 3;
    
                }
                label1.ForeColor = Color.FromArgb(180, 180, blueTasksBoard);
            }

    So what I'm trying to do here is, when the user clicks on a button this timer will start and the Blue Property of Label1 s 

    ForeColor will periodically increment by 3 until it had reached 180. And I have to put this code every time I want to change 

    the RGB of a certain Labels Forecolor, using different timers. So I have tried to write a function that I can call to, and this is what I have so Far. 

    private void stoptimer(string ColorType, int ColorNumber, int incrementalValue, string Timername, string LabelName, int red, string int, string int)
            {
                if(Int32.Parse(ColorType) <= ColorNumber)
                {
                    Timername.Stop(); //Gives me an error message
                }
                else
                {
                    int ColorTypeInt = Int32.Parse(ColorType);
    
                    ColorTypeInt = ColorTypeInt - 3;
                }
                LabelName.ForeColor = Color.FromArgb(red, green, blue); //Also Gives me an Error
            }
    

    Any Suggestions on how to make a better Function? 

    Thank you

    Wednesday, July 31, 2019 10:55 PM

Answers

  • Greetings Frozen_Nova.

    This is a tougher assignment than it looks at first glance. The problem is the last line, knowing which value to change in the Color.FromArgb argument list. I think you might have to leave that line out of your new method and call it as-is in the timer tick.

    I don't want to write the whole thing for you, as this looks like homework, but I can tell you the reasons for your errors. You don't pass arguments like Label or Timer by their name, you pass the actual objects. Here's a simple example.

    // A method to stop a Timer and set the colour of a Label.
    void MyMethod(Timer timer, Label label)
    {
       timer.Stop();
       label.ForeColor = Color.FromArgb(180, 180, 180);
    }
    
    // Call like this.
    private void Timer1_Tick(object sender, EventArgs e)
    {
       // 'sender' is the Timer that triggered the event.
       MyMethod((Timer)sender, label1);
    }


    • Edited by Ante Meridian Thursday, August 1, 2019 1:20 AM Lebel, not TextBox.
    • Proposed as answer by cheong00Editor Thursday, August 1, 2019 1:24 AM
    • Marked as answer by Frozen_Nova Friday, August 2, 2019 12:53 AM
    Thursday, August 1, 2019 1:13 AM
  • Hi Frozen_Nova,

    Thank you for posting here.

    For your question, you want to write a function to change label.ForeColor.

    You could try the following code.

       private void Timer1_Tick(object sender, EventArgs e)
            {
                stoptimer(180,3,timer1,label1,180,180,180);
            }
            private void Button1_Click(object sender, EventArgs e)
            {
                timer1.Start();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Interval = 300;
            }
            private void stoptimer( int ColorNumber, int incrementalValue, Timer timer, Label label, int red, int green, int blue)
            {
                if (blue >= ColorNumber)
                {
                    timer.Stop(); 
                }
                else
                {
                    blue = blue + incrementalValue;
                }
                label.ForeColor = Color.FromArgb(red, green, blue); 
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Frozen_Nova Friday, August 2, 2019 12:53 AM
    Thursday, August 1, 2019 6:25 AM
    Moderator

All replies

  • Hello,

    I would be wonderful if we knew what the error.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, July 31, 2019 11:25 PM
    Moderator
  • Hi,

    This is the error

    Thursday, August 1, 2019 12:23 AM
  • Greetings Frozen_Nova.

    This is a tougher assignment than it looks at first glance. The problem is the last line, knowing which value to change in the Color.FromArgb argument list. I think you might have to leave that line out of your new method and call it as-is in the timer tick.

    I don't want to write the whole thing for you, as this looks like homework, but I can tell you the reasons for your errors. You don't pass arguments like Label or Timer by their name, you pass the actual objects. Here's a simple example.

    // A method to stop a Timer and set the colour of a Label.
    void MyMethod(Timer timer, Label label)
    {
       timer.Stop();
       label.ForeColor = Color.FromArgb(180, 180, 180);
    }
    
    // Call like this.
    private void Timer1_Tick(object sender, EventArgs e)
    {
       // 'sender' is the Timer that triggered the event.
       MyMethod((Timer)sender, label1);
    }


    • Edited by Ante Meridian Thursday, August 1, 2019 1:20 AM Lebel, not TextBox.
    • Proposed as answer by cheong00Editor Thursday, August 1, 2019 1:24 AM
    • Marked as answer by Frozen_Nova Friday, August 2, 2019 12:53 AM
    Thursday, August 1, 2019 1:13 AM
  • Hi Frozen_Nova,

    Thank you for posting here.

    For your question, you want to write a function to change label.ForeColor.

    You could try the following code.

       private void Timer1_Tick(object sender, EventArgs e)
            {
                stoptimer(180,3,timer1,label1,180,180,180);
            }
            private void Button1_Click(object sender, EventArgs e)
            {
                timer1.Start();
            }
            private void Form1_Load(object sender, EventArgs e)
            {
                timer1.Interval = 300;
            }
            private void stoptimer( int ColorNumber, int incrementalValue, Timer timer, Label label, int red, int green, int blue)
            {
                if (blue >= ColorNumber)
                {
                    timer.Stop(); 
                }
                else
                {
                    blue = blue + incrementalValue;
                }
                label.ForeColor = Color.FromArgb(red, green, blue); 
            }

    Result:

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Marked as answer by Frozen_Nova Friday, August 2, 2019 12:53 AM
    Thursday, August 1, 2019 6:25 AM
    Moderator
  • Thank you!
    Friday, August 2, 2019 12:55 AM
  • Thank you this helped me out a lot!
    Friday, August 2, 2019 12:55 AM