WinForm点击按钮在对应的panel里画图

  panel在form1里,button在form1上方,panel在下面。

  主要是在button1的click时间获取panel的画笔。

  下面的不行,在panel里获取画笔,然后传到button1,根本不行,因为程序的逻辑是,先点击button1加载数据,关键是怎么调用panel1_Paint方法,不知道如何调用,那就没办法同时获取panel的画笔,同理,因为此时在panel1化数据的话还没有点击button1加载数据,所以dataProject是null,而且图形根本出不来,加一个判断就ok了。

 private void panel1_Paint(object sender, PaintEventArgs e)
{
            Graphics g = e.Graphics;
            if (dataProject != null)
            {
                CreateGantt(g, dataProject);
            }
}

==============================================================

  下面就ok。

private void button1_Click(object sender, EventArgs e)
{
            ////string path = Directory.GetCurrentDirectory();
            String json = ReadJSON("E:\VS-work\Gantt\Gantt\project.txt");
            dataProject = (Hashtable)Test.JSON.Decode(json);
            Graphics g = this.panel1.CreateGraphics();
            CreateGantt(g, dataProject);
}
原文地址:https://www.cnblogs.com/hxsyl/p/4738444.html