0

I hope you can help me and this is a big code, so I'll try to explain it short. I'm making a program that can place logic gates and that can connect to each other, I'm using the OnPaint(PaintEventArgs e) method and I have troubles with connecting gates with a line. (the code is also really chaotic) I add my gates in a List private List<Tuple<Point, int>> droppedShapes = new List<Tuple<Point, int>>(); And my lines in private List<Tuple<Point, Point>> allLines = new List<Tuple<Point, Point>>(); So I can save the x-y coordinates. I use buttons to connect my lines, so on each end of connection I got a button that I can click. But I can only connect the 2 inputs of one component to each other. Here is my code, I hope you can help me!

Here you can see how I draw my components and buttons.

 protected override void OnPaint(PaintEventArgs e)
    {
        base.OnPaint(e);
        screen.Clear(Color.Black);

        Pen whitePen = new Pen(Color.White);
        SolidBrush tealBrush = new SolidBrush(Color.Teal);
        SolidBrush whiteBrush = new SolidBrush(Color.White);

        SolidBrush yellowBrush = new SolidBrush(Color.Yellow);

        SolidBrush grayBrush = new SolidBrush(Color.Gray);
        SolidBrush redBrush = new SolidBrush(Color.Red);


        foreach (var line in allLines)
        {
            screen.DrawLine(whitePen, line.Item1, line.Item2);
        }

        foreach (var pair in this.droppedShapes)
        {
            var shapeType = pair.Item2; // Reveal your own shape object here
            var location = pair.Item1;

            switch (shapeType) // Reveal your own shape object here
            {
                case 0:
                    screen.DrawRectangle(whitePen, location.X - 10, location.Y - 10, 20, 20);
                    screen.DrawString("&", new Font(FontFamily.GenericMonospace, (float)18), whiteBrush, location.X - 11, location.Y - 13);
                    screen.DrawLine(whitePen, location.X - 20, location.Y + 6, location.X - 10, location.Y + 6);
                    screen.DrawLine(whitePen, location.X - 20, location.Y - 6, location.X - 10, location.Y - 6);
                    screen.DrawLine(whitePen, location.X + 20, location.Y, location.X + 10, location.Y);

                    newB = new Point(location.X - 25, location.Y + 2);
                    newB2 = new Point(location.X - 25, location.Y - 10);
                    newB3 = new Point(location.X + 20, location.Y - 3);
                    button1 = new Button();
                    button2 = new Button();
                    button3 = new Button();
                    button1.Size = new Size(8, 8);
                    button2.Size = new Size(8, 8);
                    button3.Size = new Size(8, 8);
                    newB.Offset(0, 0);
                    newB2.Offset(0, 0);
                    newB3.Offset(0, 0);
                    button1.Location = newB;
                    button2.Location = newB2;
                    button3.Location = newB3;
                    button1.Click += Button_Click1;
                    button2.Click += Button_Click2;
                    button3.Click += Button_Click3;
                    drawPanel.Controls.Add(button1);
                    drawPanel.Controls.Add(button2);
                    drawPanel.Controls.Add(button3);


                    if (jaOfNee == true)
                    {
                       screen.DrawString(pair.Item1.ToString(), new Font(FontFamily.GenericMonospace, (float) 6), whiteBrush, location.X - 25, location.Y - 25);
                    }
                    break;


                case 1:
                    screen.DrawRectangle(whitePen, location.X - 10, location.Y - 10, 20, 20);
                    screen.DrawString("1", new Font(FontFamily.GenericMonospace, (float)10), whiteBrush, location.X, location.Y - 9);
                    screen.DrawString(">", new Font(FontFamily.GenericMonospace, (float)8), whiteBrush, location.X - 8, location.Y - 10);
                    screen.DrawLine(whitePen, location.X + 1, location.Y + 1, location.X - 5, location.Y + 1);
                    screen.DrawLine(whitePen, location.X - 20, location.Y + 6, location.X - 10, location.Y + 6);
                    screen.DrawLine(whitePen, location.X - 20, location.Y - 6, location.X - 10, location.Y - 6);
                    screen.DrawLine(whitePen, location.X + 20, location.Y, location.X + 10, location.Y);
                    newB = new Point(location.X - 25, location.Y + 2);
                    newB2 = new Point(location.X - 25, location.Y - 10);
                    newB3 = new Point(location.X + 20, location.Y - 3);
                    button1 = new Button();
                    button2 = new Button();
                    button3 = new Button();
                    button1.Size = new Size(8, 8);
                    button2.Size = new Size(8, 8);
                    button3.Size = new Size(8, 8);
                    newB.Offset(0, 0);
                    newB2.Offset(0, 0);
                    newB3.Offset(0, 0);
                    button1.Location = newB;
                    button2.Location = newB2;
                    button3.Location = newB3;
                    button1.Click += Button_Click1;
                    button2.Click += Button_Click2;
                    button3.Click += Button_Click3;
                    drawPanel.Controls.Add(button1);
                    drawPanel.Controls.Add(button2);
                    drawPanel.Controls.Add(button3);

                    if (jaOfNee == true)
                    {
                        screen.DrawString(pair.Item1.ToString(), new Font(FontFamily.GenericMonospace, (float) 6), whiteBrush, location.X - 25, location.Y - 25);

                    }
                    break;

                default:
                    break;

            }

        }


        for (int i = drawPanel.Left - 5; i < drawPanel.Right + 10; i += 5)
        {
            for (int j = drawPanel.Top - 50; j < drawPanel.Bottom - 5; j += 5)
            {
                screen.FillEllipse(redBrush, i, j, 2, 2); //fills screen with a grid
            }
        }



        //  base.OnPaint(e);

        // draw current location
        if (currentLocation != null)
        {
            Point p = currentLocation.Value;
            screen.FillEllipse(tealBrush, p.X + 1, p.Y, -4, -4);
        }

        drawPanel.CreateGraphics().DrawImage(backBuffer, 0, 0); //allows you to draw
    }
}

(This is how I try to connect my 2 buttons)

 foreach (var line in allLines)
    {
        screen.DrawLine(whitePen, line.Item1, line.Item2);
    }

I don't use button 3 yet because I want to connect my inputs first so don't mind button 3. This is how I try to save my X-Y coordinates.

private Point point1;
    private Point point2;

    private void Button_Click1(object sender, EventArgs e)
    {
        Console.WriteLine("button1");
        point1 = button1.Location;
    }

    private void Button_Click2(object sender, EventArgs e)
    {
        Console.WriteLine("button2");
        point2 = button2.Location;

    }

    private void Button_Click3(object sender, EventArgs e)
    {
        Console.WriteLine("button3");
    }

    private void DrawLine_Click(object sender, EventArgs e)
    {
        allLines.Add(new Tuple<Point, Point>(point1, point2));
    }

After I press the button DrawLine it adds the location to the list. Sorry for the horrible code. I hope you understand my question a little bit more, it's really hard to explain my problem.

Amad
  • 25
  • 9
Vengenecx
  • 105
  • 8
  • 3
    Never add controls in a paint event. Paint events are for painting only. You are going to have to re-think your approach. If the lines need to be drawn "over" other controls, then you would have to have a transparent panel or form above all that, and draw on that. Not easy. – LarsTech Dec 02 '18 at 15:41
  • 2
    You should be using the `e.Graphics` object for drawing, not a mysterious `screen` object. It would also make your code more readable if you used dedicated Line and Gate classes instead of tuples. They could have a `void Draw(Graphics g)` method to draw themselves, leading to a better code structure. – Olivier Jacot-Descombes Dec 02 '18 at 15:46

0 Answers0