0

Im trying to create easy drawing program in Visual Studio 2015 (c#). It just about drawing lines to create some kind of shape. In forst Form i draw shapes, and i want to copy this shape to another Form where i can work on it (do some math with it). The problem is i dont know how to copy created shape (and all other data) to second Form.

Could you help me?

public partial class Form1 : Form
{



    Form2 secondForm = new Form2();

    Graphics gObject;
    Brush BBrush = new SolidBrush(Color.Black);
    Pen BPen = new Pen(Color.Black, 2);
    int xs = 100, ys = 100; //starting points
    int move;           
    int xt = 0, yt = 0;       // temporary points

        public void button1_Click(object sender, EventArgs e) 
    {

        if (xt == 0 & yt ==0)
        {

           gObject.DrawLine(BPen, xs, ys, xt = xs + move, ys);



            secondForm.xs = xs;
            secondForm.ys = ys;
            secondForm.xt = xt;
            secondForm.yt = yt;
            secondForm.move = move;


            // Draw same line in second Form

            secondForm.gObject = secondForm.DrawingPanel.CreateGraphics();

            secondForm.gObject.DrawLine(BPen, xs, ys, xt = xs + move, ys);

        }
mymek
  • 1
  • 3
  • _secondForm.DrawingPanel.CreateGraphics();_ This is nver how you should draw. Instead bring the data ie coordinates, colors, width etc to the other form and use them in its DrawingPanel.Peint event, which you also need totrigger by Invalidating it. You need references to the other form, its drawpanel and you should make it public (modifiers in the propeties panel..) – TaW Nov 28 '16 at 22:09
  • You are looking probably looking for a "deep copy" of the objects containing your data. [This page](https://msdn.microsoft.com/en-us/library/system.object.memberwiseclone(v=vs.110).aspx) appears to be the one MSDN page that explains the difference between "shallow" and "deep" copying of an object. Currently this line: `secondForm.gObject.DrawLine(BPen, xs, ys, xt = xs + move, ys);` is already creating a copy, but it has a shallow copy of `BPen` and a deep copy of everything else because `int` is a value type which is automatically "deep" copied. – Quantic Nov 28 '16 at 22:22
  • @TaW , I was thinking about easiest way to do this, but like You said, best way is to find a way to save every move, coordinate etc. that was made in Form1, and copy it to second Form, but dont know how to do this - with vectors, arrays, database? – mymek Nov 29 '16 at 10:23
  • Depending on how complex your drawings are you may get away with a List or need to create a class, say, `DrawAction` that hold point list, pens or Brishes etc..and pass Lists of that.. – TaW Nov 29 '16 at 10:44

0 Answers0