Restarting timer doesnt work

Zed McJack

New member
Joined
Oct 16, 2014
Messages
2
Programming Experience
10+
Hi all,
I was going through getting started turorials on MS site as I downloaded C# Express 2010.
Finished second tutorial and wanted to add a couple of things, one of them is timer.
Added timer and it works as expected, but restarting it doesnt work. Dont know what I did wrongly?
C#:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Lavirint
{
    public partial class Form1 : Form
    {
        int ProtekloVreme = 0;
        System.Media.SoundPlayer ZidPlejer = new System.Media.SoundPlayer(@"c:\windows\media\ding.wav");
        System.Media.SoundPlayer CiljniPlejer = new System.Media.SoundPlayer(@"c:\windows\media\tada.wav");
        public Form1()
        {
            InitializeComponent();
            idi_na_pocetak(0);
        }
        /// <summary>
        /// Pomera kursor na početnu poziciju ako 
        /// igrač dodirne zid
        /// </summary>
        private void idi_na_pocetak(int Zvuk_ili_ne)
        {
            lblVreme.Text = "Vreme: 0";
            if (Zvuk_ili_ne == 1)
            {
                ZidPlejer.Play();
            }
            
            Point startingpoint = panel1.Location;
            startingpoint.Offset(40,40);            
            Cursor.Position = PointToScreen(startingpoint);
            timer1.Enabled = true;
        }
        private void cilj_MouseEnter(object sender, EventArgs e)
        {
            CiljniPlejer.Play();
            MessageBox.Show("Čestitamo!");
            Close();
        }

        private void zid_MouseEnter(object sender, EventArgs e)
        {
            idi_na_pocetak(1);
            timer1.Enabled = false;
            ProtekloVreme = 0;
        }

        private void brojac(object sender, EventArgs e)
        {
            ProtekloVreme += 1;
            lblVreme.Text = "Vreme: " + ProtekloVreme.ToString();
        }

    }
}
 

wim sturkenboom

Well-known member
Joined
Aug 6, 2014
Messages
85
Location
Roodepoort, South Africa
Programming Experience
10+
In idi_na_pocetak, you enable the timer. idi_na_pocetak is called in two places, the constructor of the form and in zid_MouseEnter.

For the constructor it all works. But what happens when the code in zid_MouseEnter is executed? After calling idi_na_pocetak (and enabling the timer in that method), you disable the timer. None of your other methods enable the timer again so it will never start running.

Maybe the sequence in zid_MouseEnter should be swapped around?
 

Zed McJack

New member
Joined
Oct 16, 2014
Messages
2
Programming Experience
10+
Hey thanks!,
C#:
        private void zid_MouseEnter(object sender, EventArgs e)
        {
            timer1.Enabled = false;
            ProtekloVreme = 0;
            idi_na_pocetak(1);

        }
that swap actually solved the problem, and it took me a couple of minutes to actually understand why it didnt work lol. You were right I actually first enabled the timer in the idi_na_pocetak() and then disabled it in zidMouseEnter() when all of the code from idi_na_pocetak() was executed.

Thanks again. :)
 
Top Bottom