The function that my timer carries out:

1) changes the text of buttonX4 from "Copy To Clipboard" to "Your Bio Has Been Copied"
2) after 2 seconds (timer steps in) it changes from "Your Bio Has Been Copied" to "Copy To Clipboard"

Problem: After it completes step 2 when i click the button again the timer doesnt function. It only shows "Your Bio Has Been Copied" for a fraction of a second

How do i make it so that in my code the timer gets reset so when i click the button again it carries out steps 1 and 2 again.

MY CODE

public void buttonX4_Click(object sender, EventArgs e)
        {
      
            buttonX4.Text = "Your Bio Has Been Copied";



            timer1.Start();


        }
private void timer1_Tick(object sender, EventArgs e)
        {
            l_time = new System.Timers.Timer(2000);
            l_time.AutoReset = false;
            l_time.Start();
            //System.Threading.Thread.Sleep(5000);
            // l_time == Elapsed;

            l_time.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        }


        public void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            //buttonX4.Text = "Copy To Clipboard";
            if (buttonX4.InvokeRequired)
            {
                this.Invoke((MethodInvoker)delegate()
                {
                    buttonX4.Text = "Copy To Clipboard";
                });
            }
            l_time.Stop();
        }

Try this,

public void buttonX4_Click(object sender, EventArgs e)
        {
           button1.Invoke(new Action(()=>
                {
                    button1.Text = "Your Bio Has Been Copied";
                    button1.Refresh();
                    System.Threading.Thread.Sleep(2000);
                    button1.Text = "Copy To Clipboard";
                    button1.Refresh();
                }));       
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.