I want this simple demo program to draw 3 lines at intervals of 500 msec between them [(i.e. draw line one (pause interval 500msec), draw line two (pause interval 500msec), draw line 3 and finally stop timer1].
I have already entered the value 500msec in the corresponting (timer1 property Behavior Interval) field on visual studio. As it is now, the demo program does draw the three lines, but the interval of 500 msec does not work (obviously because there is code missing in void timer1_Tick.
namespace WindowsFormsApp44
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Graphics g;
Pen p;
private void Form1_Load(object sender, EventArgs e)
{
g = this.CreateGraphics();
p = new Pen(Color.Red, 5);
}
private void button1_Click(object sender, EventArgs e)
{
timer1.Start();
g.DrawLine(p, 200, 200, 300, 100);
g.DrawLine(p, 300, 100, 400, 200);
g.DrawLine(p, 200, 200, 400, 200);
timer1.Stop();
}
private void timer1_Tick(object sender, EventArgs e)
{
}
}
}
Try something like this:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApp13
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Graphics g;
Pen p;
int tickcount = 1;
private void button1_Click(object sender, EventArgs e)
{
timer1.Enabled = true;
timer1.Start();
}
private void Form1_Load(object sender, EventArgs e)
{
g = this.CreateGraphics();
p = new Pen(Color.Red, 5);
}
private void timer1_Tick(object sender, EventArgs e)
{
switch (tickcount)
{
case 1:
g.DrawLine(p, 200, 200, 300, 100);
break;
case 2:
g.DrawLine(p, 300, 100, 400, 200);
break;
case 3:
g.DrawLine(p, 200, 200, 400, 200);
break;
default:
break;
}
tickcount++;
if (tickcount > 3)
{
timer1.Stop();
timer1.Enabled = false;
}
}
}
}