c#自定义控件调试的时候代码无效


using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Drawing.Drawing2D;

namespace Chart
{
    public partial class Chart: UserControl
    {
        public Chart()
        {
            InitializeComponent();
            init();
        }

        private void Chart_Load(object sender, EventArgs e)
        {


        }

        private void button1_Click(object sender, EventArgs e)
        {
            Graphics Gp = this.CreateGraphics();
            Pen pen = new Pen(Color.Red);
            pen.Width = 2;
            Point startPoint = new Point(20, 20);
            Point endPoint = new Point(70, 20);
            Gp.DrawLine(pen, startPoint, endPoint);
        }

        private void init()
        {
            Graphics Gp = this.CreateGraphics();
            Pen pen = new Pen(Color.Red);
            pen.Width = 2;
            Point startPoint = new Point(20, 20);
            Point endPoint = new Point(70, 20);
            Gp.DrawLine(pen, startPoint, endPoint);
        }
    }
}

img

启动时

img


按下button时

为什么init方法无效呢?

自定义控件一般 要在 protected override void OnPaint(PaintEventArgs pe) 重绘

按下button执行的是 private void button1_Click(object sender, EventArgs e)啊!为什么会调试init方法呢?

init(); 放在Chart_Load 试试