面板上画线没有显示出来

面板上画线没有显示出来

问题描述:

我有一个小组叫PANEL1,我试图借鉴使用此代码我PANEL1一行:

I have a Panel called panel1 and I am trying to draw a line on my panel1 using this code:

Graphics g = panel1.CreateGraphics();
        var p = new Pen(Color.Black, 3);
        var point1 = new Point(234,118);
        var point2 = new Point(293,228);
        g.DrawLine(p, point1, point2);



但没有什么是显示出来。有任何想法吗?这是一个Windows窗体。

But nothing is showing up. Any ideas? This is in a windows form.

处理小组的 Paint事件,并把它放在那里。发生了什么事是,它正在绘制一旦在构造函数,但随后被抽过在油漆事件,每次它被称为

Handle the Panel's Paint event and put it in there. What's happening is that it's being drawn once in the constructor but then being drawn over in the Paint event everytime it's called.

private void panel1_Paint(object sender, PaintEventArgs e)
{
    base.OnPaint(e);
    using(Graphics g = e.Graphics)
    {
       var p = new Pen(Color.Black, 3);
       var point1 = new Point(234,118);
       var point2 = new Point(293,228);
       g.DrawLine(p, point1, point2);
    }
}