0gravity
0gravity

Reputation: 2762

Calling a method from another class inside Panel_Paint does not draw anything

So this should be very simple but I have looked at some similar questions and can't find an answer.

I have a Form1 class and a Resistor class. Inside the Form1 class I have a Panel(I changed the name to Canvas), inside the Canvas_Paint method I am calling the method Draw from the Resistor class but is not drawing anything.

Form1 Class:

public partial class Form1 : Form
{
    static float lineWidth = 2.0F;
    static float backgroundLineWidth = 2.0F;
    static Pen pen = new Pen(Color.Yellow, lineWidth);
    static Pen backgroundPen = new Pen(Color.LightGray, backgroundLineWidth);
    private bool drawBackground = true;
    private List<Resistor> resistors = new List<Resistor>();                

    public Form1()
    {
        InitializeComponent();
    }

    private void Canvas_Paint(object sender, PaintEventArgs e)
    {
        if (drawBackground)
        {
            Console.WriteLine("Drawing background...");
            Draw_Background(e.Graphics, backgroundPen);
        }

        if (resistors != null)
        {
            foreach (Resistor r in resistors)
            {
                //This does not work.
                r.Draw(e.Graphics);
            }
        }

        //The line below draws the line fine.
        e.Graphics.DrawLine(pen, 0, 0, 100, 100);
    }

    private void Draw_Background(Graphics g, Pen pen)
    {            
        for (int i = 0; i < Canvas.Width; i += 10)
        {
            g.DrawLine(pen, new Point(i, 0), new Point(i, Canvas.Height));          
        }
        for (int j = 0; j < Canvas.Height; j += 10)
        {
            g.DrawLine(pen, new Point(0, j), new Point(Canvas.Width, j));
        }

        drawBackground = false;
    }

    private void AddResistor_Click(object sender, EventArgs e)
    {
        resistors.Add(new Resistor());
        Console.WriteLine("Added a Resistor...");
    }        
}

Resistor Class:

public class Resistor
{
    static private Point startingPoint;
    static Pen defaultPen;
    private Point[] points;

    public Resistor()
    {
        startingPoint.X = 100;
        startingPoint.Y = 100;

        defaultPen = new Pen(Color.Yellow, 2.0F);

        points = new Point[] {
            new Point( 10,  10),
            new Point( 10, 100),
            new Point(200,  50),
            new Point(250, 300) 
        };

    }

    public void Draw(Graphics g)
    {
        //Is this drawing somewhere else?
        g.DrawLines(defaultPen, points);            
    }
}

I have looked at this question which suggests to pass the e.Graphics object in this case to the Draw method in the Resistor class but is not working.

I am new to C# so I would really appreciate any help.

EDIT : I put the project on github if you want to download and try it out.

EDIT : So the problem was that after clicking the button the panel Paint method was not being called. The solution was to add Canvas.Invalidate inside the AddResistor_Click method

Upvotes: 0

Views: 1170

Answers (2)

0gravity
0gravity

Reputation: 2762

The problem was that when the button was clicked the panel's paint method was not getting called because I tough that the paint method was always getting called. The solution was to add Canvas.Invalidate inside the AddResistor_Click method.

private void AddResistor_Click(object sender, EventArgs e)
    {
        resistors.Add(new Resistor());
        Console.WriteLine("Added a Resistor...");
        Canvas.Invalidate();
    }

Upvotes: 0

Jason Williams
Jason Williams

Reputation: 57932

Run your code in the debugger and put a breakpoint in your event handler and you'll be able to check that your code is actually trying to draw something. If not, then is your event handler ever called? is there anything in your list of resistors? If it is drawing but you don't see anything, then you're not using the correct Graphics context or you're not drawing things in the visible part of your control, or you're drawing over things with subsequent drawing code.

Upvotes: 1

Related Questions