0

Hi guys I'm quite new to programming and this is actually my first time here.

I'm creating a C# sharp web form for displaying route information by reading from MySQL database. I want to create a method which draws the lines on the map between the connecting points. The code I have so far only draws the points and their names but does not link them.

private void DrawLines()
{

        int start1;
        int start2;
        int end1;
        int end2;

        Graphics g = this.CreateGraphics();
        Pen white = new Pen(Color.White, 1);
        Pen blackW = new Pen(Color.Black, 3);
        Pen goldW = new Pen(Color.Gold, 3);
        Pen redW = new Pen(Color.Red, 3);
        Pen blueW = new Pen(Color.Blue, 3);

        g.FillRectangle(white.Brush, new Rectangle(0, 0, 800, 600));



        for (int i = 0; i < lineCount; i++)
        {
            for (int j = 1; j < stationCount; j++)
            {
                int stationID1 = stationNumber[i, j];
                int stationID2 = stationNumber[i, j + 1];
                if (stationID2 > 0)
                {
                    start1 = 0;
                    start2 = 0;
                    end1 = 0;
                    end2 = 0;
                    for (int s = 0; s < stationCount; s++)
                    {
                        if (stationID1 == stationID[s])
                        {
                            start1 = stationX[s];
                            start2 = stationY[s];
                        }
                        if (stationID2 == stationID[s])
                        {
                            end1 = stationX[s];
                            end2 = stationY[s];
                        }
                    }
                    if (lineName[i] == "Northen")
                        g.DrawLine(blackW, start1, start2, end1, end2);

                    if (lineName[i] == "Central")
                        g.DrawLine(redW, start1, start2, end1, end2);

                    if (lineName[i] == "Victoria")
                        g.DrawLine(blueW, start1, start2, end1, end2);

                    if (lineName[i] == "Circle")
                        g.DrawLine(goldW, start1, start2, end1, end2);
                }
            }
        }
    }

And here is the MYSQL statement

 private void GetLines()
    {
        try
        {
            dbtry.Open();
            MySqlCommand cmdLines = new MySqlCommand();
            cmdLines.Connection = dbtry;
            cmdLines.CommandText = "SELECT TrainLines.ID, TrainLines.LineName, TrainStations.ID, TrainStations.StationName, StationsAndLines.ID, StationsAndLines.TrainLines_ID, StationsAndLines.TrainStations_ID, StationsAndLines.Position, StationsAndLines.Distance FROM TrainStations, TrainLines, StationsAndLines  WHERE StationsAndLines.TrainLines_ID = TrainLines.ID AND StationsAndLines.TrainStations_ID = TrainStations.ID  ORDER BY TrainLines.LineName ASC, StationsAndLines.Position ASC"; // get all the line IDs

            MySqlDataReader drLines =  cmdLines.ExecuteReader(); //create an adapter with the Line IDs
           // drLines.Fill(dsLines); //create dataset of lines IDs 
            while (drLines.Read())
            {
                miStations.Add(drLines["StationName"].ToString());
                miStations.Add(drLines["Distance"].ToString());
                miStations.Add(drLines["LineName"].ToString());
                miStations.Add(drLines["Position"].ToString());

            }

what i'm kind of trying to get

database of stations and their positions

the lined database

the current map i'm getting

Héctor M.
  • 2,302
  • 4
  • 17
  • 35
  • 2
    a) `Graphics g = this.CreateGraphics();` this will not create persistent graphics. (almost) never use `CreateGraphics` !! - Move the code to the Form's Paint event and use the e.Graphics object there!! b) use the debugger to test if you ever hit the DrawLine commands! - Or create a Bitmap and draw into it.. See [here](https://stackoverflow.com/questions/27337825/picturebox-paintevent-with-other-method/27341797?s=1|52.7218#27341797) for the difference.. – TaW Mar 23 '18 at 23:37

1 Answers1

1

So I would split the problem down into smaller chunks, I am unsure what you are trying to achieve with the multiple loops and I am sure that we could remove them by reading from the database. What you want to be doing is minimal amount of looping as possible and where possible removing the if statement. For examples Where you have

if (lineName[i] == "Northen")
             g.DrawLine(blackW, start1, start2, end1, end2);

                if (lineName[i] == "Central")
                    g.DrawLine(redW, start1, start2, end1, end2);

                if (lineName[i] == "Victoria")
                    g.DrawLine(blueW, start1, start2, end1, end2);

                if (lineName[i] == "Circle")
                    g.DrawLine(goldW, start1, start2, end1, end2);

This chunk of code can be removed. And separated out into a new method that draws the line by points 'Point(x,y)' like so

public void DrawLinePoint(String type, Graphics graphics, Point start, Point end)
    {
        //Switch statements are more more efficient than a if statement and a lot nicer to ready and add
        switch(type)
        {
            case "Northern":
                graphics.DrawLine(new Pen(Color.Black, 3), start, end);
                break;
            case "Central":
                graphics.DrawLine(new Pen(Color.Red, 3), start, end);
                break;
            case "Victoria":
                graphics.DrawLine(new Pen(Color.Blue, 3), start, end);
                break;
            case "Circle":
                graphics.DrawLine(new Pen(Color.Red, 3), start, end);
                break;
            default:
                graphics.DrawLine(new Pen(Color.White, 1), start, end);
                break;
        }            
    }

This makes it much easier to add new stations into the mix. This is what I would use for the main

 public Form1()
    {
        InitializeComponent();
       // graphics = this.drawingArea.CreateGraphics();
        this.Paint += new System.Windows.Forms.PaintEventHandler(DrawLines);
    }
    public void DrawLines(object sender, PaintEventArgs e)
    {
        Graphics g;

        g = e.Graphics;

        // Create points that define line.

        for (int i = 0; i < lineCount; i++)
        {
            for (int j = 1; j < stationCount; j++)
            {
                int stationID1 = stationNumber[i, j];
                int stationID2 = stationNumber[i, j + 1];
                if (stationID2 > 0)
                {

                    for (int s = 0; s < stationCount; s++)
                    {
                        if (stationID1 == stationID[s])
                        {
                            Point start = new Point(stationX[s], stationY[s]);
                        }
                        if (stationID2 == stationID[s])
                        {
                            Point end = new Point(stationX[s], stationY[s]);
                        }
                    }
                    DrawLinePoint(lineName[i], g, start, end);
                }
            }
        }
    }