new_leo 2021-09-15 15:21 采纳率: 50%
浏览 25

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方法无效呢?

  • 写回答

3条回答 默认 最新

  • MichaelGLX 2021-09-15 16:12
    关注

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

    评论

报告相同问题?

问题事件

  • 创建了问题 9月15日

悬赏问题

  • ¥100 网页游戏在本地页面修改游戏结果
  • ¥30 comfyui openpose报错
  • ¥20 Wpf Datarid单元格闪烁效果的实现
  • ¥15 图像分割、图像边缘提取
  • ¥15 sqlserver执行存储过程报错
  • ¥100 nuxt、uniapp、ruoyi-vue 相关发布问题
  • ¥15 浮窗和全屏应用同时存在,全屏应用输入法无法弹出
  • ¥100 matlab2009 32位一直初始化
  • ¥15 Expected type 'str | PathLike[str]…… bytes' instead
  • ¥15 三极管电路求解,已知电阻电压和三级关放大倍数