Mehrdad
Mehrdad

Reputation: 184

call Paint event of picturebox from PageLoad in C# causes an error

I create a PictureBox that get lots of point and draws a map for my application ,so these point is read from database ,so when i move the scroll of my picturebox to see other part of my map ,the paint event is called again ,and my application have to read the points from databse to draw map ,but it doesn't need ,because i read that before ,so reading data from database makes my application to be so slow ,so i try to locate my code in Paint event to Page_load ,because this transfering makes my code to be run just one time , So let me explain my code :

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());



        }

This is Pain event of my picturebox that after every scroll movement is called .I need to transfer this code to page_Load.I did that but my problem is how can i handle these values :

(object sender, PaintEventArgs e)

Because the page_load doesnt any PaintEventArgs argument.I mean how can i tranfer this code to page_load

My page_load :

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


        }

Best regards

Upvotes: 0

Views: 383

Answers (1)

Henk Holterman
Henk Holterman

Reputation: 273464

You shouldn't read from a database in OnPaint and you shouldn't draw on the screen in OnLoad.

So divide the work: in OnLoad, fetch and store the data. In OnPaint, draw the cached data.

Upvotes: 1

Related Questions