Hello,

I'm developing an application where if a user clicks a button a comparisson is performed, if the user was right an image needs to show for 1 second if the user was wrong another image needs to show for one second too.

The thing is, I don't know what should be the best solution, I tried putting the thread to sleep for one second after the image loads then hiding the image, but when I put it that way the image never shows.

I'm thinking implementing a timer, but I want to know if there is a way I'm missing that might be easy and efficient to do instead of a timer.

Thanks

I'd suggest ErrorProvider control.

...
if (GetCompareResult())
   {
   errorProvider1.Icon = new Icon(@"C:\path\correct.ico");
   }
else
  {
   errorProvider1.Icon = new Icon(@"C:\path\incorrect.ico");
  }
errorProvider1.SetError(button1 , string.Empty);
         
...

Well, that funcionality is not really what I'm looking for because I want the image to disapear imedialetly, with the error provider the image would remain until another event which does not work for me, I want the image to appear wait a second and then dissapear.

Thanks

Well, that funcionality is not really what I'm looking for because I want the image to disapear imedialetly, with the error provider the image would remain until another event which does not work for me, I want the image to appear wait a second and then dissapear.

Thanks

Use a timer perhaps?

when the user clicks the button set the value of the image and start the timer when the timer ticks make it clear the image and stop the timer.

-Matt

Try this,

private void button1_Click(object sender, EventArgs e)
        {
         if(GetCompareResult())
          {
               pictureBox1.Invoke(new Action(() =>
                {
                    pictureBox1.Image = Image.FromFile(@"C:\path\style.gif");
                    pictureBox1.Refresh();
                    System.Threading.Thread.Sleep (1000);
                    pictureBox1.Image = null;
                }));
           }
         }

Try this,

private void button1_Click(object sender, EventArgs e)
        {
         if(GetCompareResult())
          {
               pictureBox1.Invoke(new Action(() =>
                {
                    pictureBox1.Image = Image.FromFile(@"C:\path\style.gif");
                    pictureBox1.Refresh();
                    System.Threading.Thread.Sleep (1000);
                    pictureBox1.Image = null;
                }));
            }
           }
         }

Only problem I can see with that is that it locks up the whole form, that's okay if it only has to show the image but if it needs to do something else after then it has to wait for the image to disappear, I suppose it doesn't really matter as it is only displaying for a second.

The code for using a timer to do it is below, this allows the form to be used still while the image is showing.

private void button1_Click(object sender, EventArgs e)
        {
         if(GetCompareResult())
          {
                pictureBox1.Image = Image.FromFile(@"C:\path\style.gif");
                pictureBox1.Refresh();
                timer1.Interval = 1000;
                timer1.Start();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            pictureBox1.Image = null;
            timer1.Stop();
        }

Ok, I implemented like thtat.

THank You

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.