What i want to do is that when i click buttonX4 i want the text to change from "Copy To Clipboard" to "Your Bio Has Been Copied" Then i want it to wait for 2 seconds then i want it to change back to "Copy To Clipboard". I did this with the assist of a timer. It all works until it goes to

public void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            buttonX4.Text = "Copy To Clipboard";

        }

after this this message comes up : "Cross-thread operation not valid: Control 'buttonX4' accessed from a thread other than the thread it was created on."
How can i fix it ?

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();
            l_time.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        }


        public void OnTimedEvent(object source, ElapsedEventArgs e)
        {
            buttonX4.Text = "Copy To Clipboard";

        }

Use this:

if(buttonX4.InvokeRequired)
{
  this.Invoke((MethodInvoker)delegate()
  {
       buttonX4.Text = "Copy To Clipboard";
  });
}
commented: Good +6
commented: helped me +0

thanks, it works.

I made a mistake in the coding though. The coding works, i.e. halts on "your bio ..ted" for 2 secs, then changes to "copy to ..rd" but then when i click it again it doesnt halt/wait on "your bio ..ted" for 2 secs it just bypasses it (well it shows for a fraction of a second)

What do i do ?

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.