从C#中的PageLoad调用picturebox的Paint事件会导致错误

本文关键字:事件 错误 Paint 中的 PageLoad 调用 picturebox | 更新日期: 2023-09-27 18:06:18

我创建了一个PictureBox,它获得了很多点,并为我的应用程序绘制了一张地图,所以这些点是从数据库中读取的,所以当我移动PictureBox的滚动以查看我的地图的其他部分时,paint事件会再次调用,我的应用软件必须从数据库中读出点才能绘制地图,但它不需要,因为我以前读过,所以从数据库读取数据会让我的应用程序非常慢,所以我试图在Paint eventPage_load中定位我的代码,因为这种传输使我的代码只运行一次,让我解释一下我的代码:

private void pictureBoxMetroMap_Paint(object sender, PaintEventArgs e)
        {
            Pen pLine = new Pen(Color.White, 2);
            SolidBrush RedBrush = new SolidBrush(ColorTranslator.FromHtml("#cc0000"));
            SolidBrush blueBrush = new SolidBrush(ColorTranslator.FromHtml("#0066ff"));
            SolidBrush greenBrush = new SolidBrush(ColorTranslator.FromHtml("#3db300"));
            SolidBrush whiteBrush = new SolidBrush(ColorTranslator.FromHtml("#fff"));

            SensorRepository objSensorRepository = new SensorRepository();
            List<Sensor> lstSensorLeft = objSensorRepository.GetSensorsLine(1, "Left");
            List<Point> lstPointLeft = new List<Point>();
            foreach (var t in lstSensorLeft)
            {
                Point objPoint = new Point(t.XLocation, t.YLocation);
                lstPointLeft.Add(objPoint);
                Rectangle rectSens = new Rectangle(t.XLocation, t.YLocation, 3, 3);
                e.Graphics.FillRectangle(whiteBrush, rectSens);
                if (t.StationId != null)
                {
                    Rectangle rectEhsansq = new Rectangle(t.XLocation-6, t.YLocation-6, 12, 12);
                    e.Graphics.FillRectangle(blueBrush, rectEhsansq);
                    Rectangle rectTrainState = new Rectangle(t.XLocation -3, t.YLocation-3, 7, 7);
                    e.Graphics.FillRectangle(RedBrush, rectTrainState);
                }
            }
            StationRepository ObjStationRepository = new StationRepository();
            List<Sensor> lstSensorRight = objSensorRepository.GetSensorsLine(1, "Right");
            List<Point> lstPointRight = new List<Point>();
            foreach (var t in lstSensorRight)
            {
                Point objPoint = new Point(t.XLocation+30, t.YLocation+30);
                lstPointRight.Add(objPoint);
                Rectangle rectSens = new Rectangle(t.XLocation+30, t.YLocation+30, 3, 3);
                e.Graphics.FillRectangle(whiteBrush, rectSens);
                if (t.StationId!= null)
                {
                    Rectangle rectPosition= new Rectangle(t.XLocation + 24, t.YLocation + 24, 12, 12);
                    e.Graphics.FillRectangle(blueBrush, rectPosition);
                    Rectangle rectTrainState = new Rectangle(t.XLocation + 27, t.YLocation + 27, 7, 7);
                    e.Graphics.FillRectangle(RedBrush, rectTrainState);
                }
            }

            e.Graphics.DrawLines(pLine, lstPointLeft.ToArray());
            e.Graphics.DrawLines(pLine, lstPointRight.ToArray());

        }

这是我的picturebox的疼痛事件,每次滚动后都会调用它。我需要将此代码转移到page_Load。我做到了,但我的问题是如何处理这些值:

(object sender, PaintEventArgs e)

因为page_load没有任何PaintEventArgs参数。我的意思是如何将此代码传输到page_load

我的页面加载:

 private void frmMain_Load(object sender, EventArgs e)
        {
            FormBorderStyle = FormBorderStyle.None;

        }

向致以最良好的问候

从C#中的PageLoad调用picturebox的Paint事件会导致错误

您不应该在OnPaint中读取数据库,也不应该在OnLoad中在屏幕上绘制。

因此,要分工:在OnLoad中,获取并存储数据。在OnPaint中,绘制缓存的数据。