点击button,在panel上画图
大家好,我再Form中添加了个panel,然后在form中添加了一个button,我想点击button后,在panel中画线,我是利用bool型数值来判断的,
代码是这个样子的:
public partial class Form1 : Form
{
bool drawline = false;
public Form1()
{
InitializeComponent();
}
private void panel1_Paint(object sender, PaintEventArgs e)
{
Graphics dl;
dl = panel1.CreateGraphics();
Point apoint = new Point(100, 200);
Point bpoint = new Point(150, 100);
if (drawline)
{
dl.DrawLine(Pens.Black, apoint,bpoint);
}
}
private void button1_Click(object sender, EventArgs e)
{
drawline = true;
}
}
我点击之后,没有画线;但是分步运行的时候,点击之后,drawline这个值是true;不清楚为什么,
求指教,谢谢
------解决方案--------------------
Graphics dl;
dl = panel1.CreateGraphics();
上面都去掉,直接用e.Graphic