0

I am working on windows forms in c#. i want to display a picture when I run the program and then that picture automatically disappear after few seconds. For this I made a picture box and gave it a background picture. (I also try giving picture at form load event.)

Then a timer internal property is set to 1000. I made a global variable:

int count=0;

in timer tick event I write a code:

private void timer1_Tick_1(object sender, EventArgs e)
        {
            

            count++;
            if(count==3)
            {
                pictureBox1.Visible = false;
                timer1.Stop();
            }
        }

this code does not work.

I tried on the picture click event

private void pictureBox1_Click(object sender, EventArgs e)
        {
            Thread.Sleep(1000);
            pictureBox1.Visible = false;
        }

It works but I want to do this automatically not on clicking. How can I do this?

  • Show the part which creates and *starts* timer. – Sinatr Aug 28 '20 at 07:15
  • 1
    `timer1_Tick_1` indicates there is *already* `timer1_Tick` event. Check which handler is assigned as timer event in properties (assuming now you are creating timer as form component using designer). – Sinatr Aug 28 '20 at 07:17
  • Btw, you can change timer interval (increase by 3 times) instead of using `count`. – Sinatr Aug 28 '20 at 07:21
  • @Sinatr I used timer from toolbox and double click on that to have its code where is write my coding given in question –  Aug 28 '20 at 07:21
  • 1
    Set breakpoint on `count++` line. If it doesn't hit during runtime your timer is either not enabled (means it doesn't start) or you did a mess with handlers. Check timer properties in designer. – Sinatr Aug 28 '20 at 07:24
  • Make sure your timer object is enabled, by default, it is not. –  Aug 28 '20 at 07:24
  • @LittleStar, out of curiocity could you tell us what was wrong? It may help future readers (assuming you won't delete question). Consider to [post self-answer](https://meta.stackoverflow.com/q/250204/1997232). – Sinatr Aug 28 '20 at 07:34
  • @Sinatr in the properties of timer, enable was false so i make it 'true' then it was working fine –  Aug 28 '20 at 08:01

1 Answers1

2

Try adding this to your code

    private void Form1_Load(object sender, EventArgs e)
    {
        timer1.Start();
    }
Wowo Ot
  • 1,362
  • 13
  • 21
  • 1
    If timer is added as form component, then it's more convenient to enable timer by setting properties of component in one place - using designer. `Enabled = true` [is same](https://stackoverflow.com/q/1012948/1997232) as `Start()`. – Sinatr Aug 28 '20 at 08:06