locked
array lineshape in c# 2010

    Question

  • i use this code for draw array lineshape but not work.

     ShapeContainer[] canvas = new ShapeContainer[10];
        LineShape[] line1 = new LineShape[10];
     canvas[0].Parent = this;
          // Set the ShapeContainer as the parent of the LineShape.
         line1[0].Parent = canvas[0];
         line1[0].Name = "hakan";
         line1[0].X1 = x1 + 40;
         line1[0].X2 = x1 - 570;
         line1[0].Y1 = y1 - 1;
         line1[0].Y2 = y1 - 1;
    

    please help  me for correct code.

    Wednesday, April 20, 2011 1:55 PM

Answers

  • Hello,

    Welcome to the MSDN Forum. Thanks for posting here.

    As your description, that’s all due to NullReference and the OOP.

    Before we use canvas[0] and line1[0], we need it’s reference. The following is my code for  your reference:

                    private void button3_Click(object sender, EventArgs e)

            {

                ShapeContainer[] canvas = new ShapeContainer[2];

                LineShape[] line = new LineShape[2];

                for (int i = 0; i <= 1; i++)

                {

                    canvas[i] = new ShapeContainer();

                    canvas[i].Parent = this;

                    line[i] = new LineShape();

                    line[i].Parent = canvas[i];

                    line[i].Name = "line" + i.ToString();

                    line[i].StartPoint = new Point(0, 0);

                    line[i].EndPoint = new Point(10 + i * 10, 100);

                  }

            }

    But we’d better to code like this:

                    private void button2_Click(object sender, EventArgs e)

            {

                ShapeContainer canvas = new ShapeContainer();// each form, we'd better to set one ShapeContainer, said in MSDN.

            canvas.Parent = this;

            for (int i = 0; i <= 2; i++)

            {

     

                LineShape line = new LineShape();

                line.Parent = canvas;

                line.Name = " line" + i.ToString();

                line.StartPoint = new Point(0, 0);

                line.EndPoint = new Point(100 - i * 10, 100 - i * 40);

     

            }

            }

     

    If there is not a solution for you, please do not hesitate to let us know.

    Thanks for your understanding and support.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 22, 2011 3:55 AM
    Moderator

All replies

  • Wednesday, April 20, 2011 3:02 PM
  • Did you set the Start and End point? Check it here:http://msdn.microsoft.com/en-us/library/microsoft.visualbasic.powerpacks.lineshape.aspx
    Mitja

    yes.
    in other way set staret and end point.
    ShapeContainer[] canvas = new ShapeContainer[10];
      LineShape[] line1 = new LineShape[10];
     canvas[0].Parent = this;
       // Set the ShapeContainer as the parent of the LineShape.
       line1[0].Parent = canvas[0];
       line1[0].Name = "hakan";
       line1[0].StartPoint = new System.Drawing.Point(50, 100);
       line1[0].EndPoint = new System.Drawing.Point(640, 480);
    but not work.
    
    without array draw lineshape but with array not work.
     
     
    Wednesday, April 20, 2011 9:55 PM
  • this code work.

    without array

     

    ShapeContainer canvas1 = new ShapeContainer();
      LineShape line2 = new LineShape();
    canvas1.Parent = this;
       line2.Parent = canvas1;
       line2.Name = "hakan";
       line2.X1 = x1 + 40;
       line2.X2 = x1 - 570;
       line2.Y1 = y1 - 1;
       line2.Y2 = y1 - 1;
    
    

     


    Wednesday, April 20, 2011 9:57 PM
  • hi

    when write this code in button click event occur errore.

    canvas[0].Parent = this;
      // Set the ShapeContainer as the parent of the LineShape.
      line1[0].Parent = canvas[0];
      line1[0].Name = "hakan";
      line1[0].StartPoint = new System.Drawing.Point(50, 100);
      line1[0].EndPoint = new System.Drawing.Point(640, 480);

    System.NullReferenceException was unhandled
      Message=Object reference not set to an instance of an object.
      Source=datagridhand
      StackTrace:
           at datagridhand.array3.button1_Click(Object sender, EventArgs e) in C:\Users\nm\Desktop\datagridhand\datagridhand\array3.cs:line 1747
           at System.Windows.Forms.Control.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnClick(EventArgs e)
           at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ButtonBase.WndProc(Message& m)
           at System.Windows.Forms.Button.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at datagridhand.Program.Main() in C:\Users\nm\Desktop\datagridhand\datagridhand\Program.cs:line 18
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean ignoreSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException:


    Thursday, April 21, 2011 5:02 AM
  • Hello,

    Welcome to the MSDN Forum. Thanks for posting here.

    As your description, that’s all due to NullReference and the OOP.

    Before we use canvas[0] and line1[0], we need it’s reference. The following is my code for  your reference:

                    private void button3_Click(object sender, EventArgs e)

            {

                ShapeContainer[] canvas = new ShapeContainer[2];

                LineShape[] line = new LineShape[2];

                for (int i = 0; i <= 1; i++)

                {

                    canvas[i] = new ShapeContainer();

                    canvas[i].Parent = this;

                    line[i] = new LineShape();

                    line[i].Parent = canvas[i];

                    line[i].Name = "line" + i.ToString();

                    line[i].StartPoint = new Point(0, 0);

                    line[i].EndPoint = new Point(10 + i * 10, 100);

                  }

            }

    But we’d better to code like this:

                    private void button2_Click(object sender, EventArgs e)

            {

                ShapeContainer canvas = new ShapeContainer();// each form, we'd better to set one ShapeContainer, said in MSDN.

            canvas.Parent = this;

            for (int i = 0; i <= 2; i++)

            {

     

                LineShape line = new LineShape();

                line.Parent = canvas;

                line.Name = " line" + i.ToString();

                line.StartPoint = new Point(0, 0);

                line.EndPoint = new Point(100 - i * 10, 100 - i * 40);

     

            }

            }

     

    If there is not a solution for you, please do not hesitate to let us know.

    Thanks for your understanding and support.


    Best Regards,
    Rocky Yue[MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, April 22, 2011 3:55 AM
    Moderator
  • thank you for help me. thank you very much.
    Friday, April 22, 2011 11:04 AM