Question

I am using a timer to record the location of clicks I perform in a picturebox for a specified amount of time that is timed by the timer. As a next step I added PictureBox paint event to show small circles where I hit in the pictureBox. It works perfectly but timer ticks somehow becomes disabled. Next I noticed that if I comment out the following line from the FORM InitializeComponent() function, timer starts working:

this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

Here is the timer1 and picturebox1 settings from InitializeComponent() function:

this.timer1 = new System.Windows.Forms.Timer();
        this.timer1.Interval = 1000;
        this.timer1.Tick += new System.EventHandler(this.timer1_Tick);
        // 
        // pictureBox1
        // 
        this.pictureBox1.BackColor = System.Drawing.SystemColors.ControlLightLight;
        this.pictureBox1.Image = global::Omers_project.Properties.Resources.img_002;
        this.pictureBox1.Location = new System.Drawing.Point(282, 158);
        this.pictureBox1.Name = "pictureBox1";
        this.pictureBox1.Size = new System.Drawing.Size(694, 492);
        this.pictureBox1.SizeMode = System.Windows.Forms.PictureBoxSizeMode.CenterImage;
        this.pictureBox1.TabIndex = 0;
        this.pictureBox1.TabStop = false;
        this.pictureBox1.Click += new System.EventHandler(this.pictureBox1_Click);
        this.pictureBox1.Paint += new System.Windows.Forms.PaintEventHandler(this.pictureBox1_Paint);

Also Timer1_Click and PictureBox1_Click functions are given below:

private void timer1_Tick(object sender, EventArgs e)
    {
        passes--;
        textBox_time_passed.Text = passes.ToString();
        if (passes == 0)
        {
            timer1.Stop();
            MessageBox.Show("Time is up"); 
        }

    }

private void pictureBox1_Click(object sender, EventArgs e)
    {
        MouseEventArgs eM = (MouseEventArgs)e;
        temp_storage.x = eM.X;
        temp_storage.y = eM.Y;

        myList.Add(temp_storage);
        count++;
        textBox6.Text = Convert.ToString(count);

    }

Hence I can use only one of them at a time but unable to use both functionalities at the same time. Please help me resolve this issue.

No correct solution

OTHER TIPS

I don't really know what your code looks like but I would suggest to execute timer Start() method instead of Enable = True.

Also... are you doing any code on the PictureBox paint event in a different thread?

If it still doesn't work, please provide more code so we can have a better idea.

Cheers

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top