Click here to Skip to main content
15,942,449 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to draw all line together. Thats why I calling for loop in this loop there I'm checking some conditions then draw line but it draws only one line at a time. But functionally it doing right I checked by breakpoint and messagebox but displaying only one line. Please help.
C#
private void drawAll_line()
{
    for (int i = 1; i <= Anum; i++)
    {
        if (!vAnd[i].isPin1Open)
        {
            if (vAnd[i].VpIn1)
            {
                System.Drawing.Graphics formGraphics = panel1.CreateGraphics();
                System.Drawing.Pen myPen;
                myPen = new System.Drawing.Pen(Color.Green);
                formGraphics.DrawLines(myPen, vAnd[i].pin1PointPath);
                myPen.Dispose();
                formGraphics.Dispose();
            }
            else
            {
                System.Drawing.Graphics formGraphics = panel1.CreateGraphics();
                System.Drawing.Pen myPen;
                myPen = new System.Drawing.Pen(Color.Red);
                formGraphics.DrawLines(myPen, vAnd[i].pin1PointPath);
                myPen.Dispose();
                formGraphics.Dispose();
            }
        }
        if (!vAnd[i].isPin2Open)
        {
            if (vAnd[i].VpIn2)
            {
                System.Drawing.Graphics formGraphics = panel1.CreateGraphics();
                System.Drawing.Pen myPen;
                myPen = new System.Drawing.Pen(Color.Green);
                formGraphics.DrawLines(myPen, vAnd[i].pin2PointPath);
                myPen.Dispose();
                formGraphics.Dispose();
            }
            else
            {
                System.Drawing.Graphics formGraphics = panel1.CreateGraphics();
                System.Drawing.Pen myPen;
                myPen = new System.Drawing.Pen(Color.Red);
                formGraphics.DrawLines(myPen, vAnd[i].pin2PointPath);
                myPen.Dispose();
                formGraphics.Dispose();
            }
        }
    }
}
Posted
Updated 16-Jun-10 12:49pm
v5

1 solution

The debugger is probably the best way to figure out why the lines aren't getting drawn as expected. That being said I see some strange things:

First,
for (int  i = 1; i <= Anum; i++)
arrays are 0 based, so shouldn't that be
for (int  i = 0; i < Anum; i++)
or better yet,
for (int  i = 0; i < vAnd.Length /* or vAnd.Count for a List */; i++)
?

Then, panel1.CreateGraphics() makes me wonder if you're doing the drawing in the correct place. When drawing on a Control you need to do it in the Control's OnPaint event handler which gives you the Graphics object to paint on.

Creating and disposing new Pens every time the method is called is wasteful, you could save some resources by using the static Pens.Red and Pens.Green objects.

If none of those help find the problem try putting a break point on each call to DrawLines to make sure that it's being called the expected number of times and the parameters that are being passed to it are correct.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900