How to copy Shape object through C#?
Last post 12-19-2007 2:29 AM by guy1218. 3 replies.
Sort Posts:
12-17-2007 12:47 AM
How to copy Shape object through C#?

I tried the following codes in the MouseMove event handler of the Canvas, but it does not work.

public void ParentMouseMove(object sender, MouseEventArgs e)
{
            if (shallPaint)
            {
                Shape tmp = paintShape; // paintShape is an Ellipse object which looks like a red dot. Defined in the Page_Loaded handler
                tmp.SetValue(Canvas.LeftProperty, e.GetPosition(this).X);
                tmp.SetValue(Canvas.TopProperty, e.GetPosition(this).Y);
                this.Children.Add(tmp);
                tmp = null;
            }
}

The ellipse object is not copied as I move the mouse, instead, there remains only one red dot on the canvas as I'm moving the cursor.

Anybody explains why it is like this and how to solve this problem?

Thanks a lot.

Where is my way?

guy1218

Loading...
Joined on 02-10-2007
Posts 2
12-17-2007 1:26 PM
Re: How to copy Shape object through C#?

Unfortunately you need to create a new shape.  The "Shape tmp = paintShp;" doesn't make a copy.  You'll need to copy the values one after another to make this work.  There is no Clone method (yet?) which is how I'd expect it to work. 

(If this has answered your question, "Mark as Answer")

Shawn Wildermuth
C# MVP, MCSD, Speaker and Author

Silverlight 2 Workshop
September 15-17, 2008 - Denver, CO
September 22-24, 2008 - New York, NY
http://www.silverlight-tour.com

swildermuth

Loading...
Joined on 10-13-2003
Atlanta, GA
Posts 1,174
12-19-2007 12:50 AM
Marked as Answer
Re: How to copy Shape object through C#?

Yes, you have to create a new Ellipse. Also note that you can’t simply copy paintShape’s Fill to the new Ellipse. One Brush can only be used once.

            Ellipse tmp = new Ellipse();

            //This won't work because a Brush can only be used once.

            //tmp.Fill = paintShape.Fill;

            //Since you know you need red, just create a new red Brush.

            tmp.Fill = new SolidColorBrush(Colors.Red);

            tmp.Width = paintShape.Width;

            tmp.Height = paintShape.Height;

            tmp.SetValue(Canvas.LeftProperty, e.GetPosition(this).X);

            tmp.SetValue(Canvas.TopProperty, e.GetPosition(this).Y);

            this.Children.Add(tmp);

            tmp = null;

 

shanaolanxing - Please mark the posts as answers if they help and unmark if they don't.

Yi-Lun Luo - MSFT

Loading...
Joined on 10-29-2007
Posts 1,976
12-19-2007 2:29 AM
Re: How to copy Shape object through C#?

I got it, thanks a lot. :D

Where is my way?

guy1218

Loading...
Joined on 02-10-2007
Posts 2
Page view counter