user3978484
user3978484

Reputation:

My variable is not changing as it is supposed to

My purpose is to change drawlines gradient each second in the form application. However it doesn't work. valueble counter " changing in label but not changing in form paint ..

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 WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private int counter = 1;

        private void timer1_Tick(object sender, EventArgs e)
        {
            counter++;
            if (counter >= 10)
                timer1.Stop();
            lblCountDown.Text = counter.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            counter = 0;

            timer1.Tick += new EventHandler(timer1_Tick);
            counter = new int();

            timer1.Interval = 1000; // 1 second
            timer1.Start();
            lblCountDown.Text = counter.ToString();
        } 
        private void Form1_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawLine(new Pen(Brushes.Crimson),200,200,counter ,300) ;
        }
    }
}

I intend to change my drawings gradient with time but variable is not changing when its

come to form paint... but it does change in lbl ...

help me if u can guys . dont know what to do.

Upvotes: 0

Views: 148

Answers (2)

Hassan
Hassan

Reputation: 5430

I will recommend following:

  1. Use Panel control for Drawing and its paint event e.g. Panel_Paint
  2. In Timer_Tick use Panel.Invalidate();
  3. In the paint event Dispose for graphic object that is Pen.

Add a Panel control named panel1 in the form. Keep every other control outside the panel.

Example of Panel Paint and Timer event:

 private void panel1_Paint(object sender, PaintEventArgs e)
  {
     using (Pen pen = new Pen(Brushes.Crimson))
        {
            e.Graphics.DrawLine(pen, 200, 200, counter, 300);                
        }
  }

 private void timer1_Tick(object sender, EventArgs e)
  {            
      counter++;
      if (counter >= 10)
          timer1.Stop();
      lblCountDown.Text = counter.ToString();
      panel1.Invalidate();           
  }

Upvotes: 0

Kirill K
Kirill K

Reputation: 51

Here, this one works. The answer is to call this.Invalidate() on form every timer tick.

public partial class Form1 : Form
{
    int counter = 0;

    public Form1()
    {
        InitializeComponent();

        timer1.Tick += new EventHandler(timer1_Tick);
    }

    private void button1_Click(object sender, EventArgs e)
    {
        counter = 1;
        timer1.Interval = 1000; // 1 second
        timer1.Start();
        lblCountDown.Text = counter.ToString();
    }

    private void timer1_Tick(object sender, EventArgs e)
    {
        counter++;
        if (counter >= 10)
            timer1.Stop();
        lblCountDown.Text = counter.ToString();
        this.Invalidate();
    }

    private void Form1_Paint(object sender, PaintEventArgs e)
    {
        e.Graphics.DrawLine(new Pen(Brushes.Crimson), 200, 200, counter*10, 300);
    }
}

Also changed several things:

  1. Event handler is set only once – to avoid multiple handlers if user clicks button several times.
  2. Removed counter = new int() – no need, you have already set it to =1.
  3. In Form1_Paint set x2 coordinate to counter*10 so it is easier to see the movement.

Upvotes: 1

Related Questions