locked
How would I go about adding multiple rectangles using the same lines of code? RRS feed

  • Question

  • How would I go about adding multiple rectangles using the same lines of code? I would prefer to just run through a set of code every time I need a polygon. If I just have to create multiple polygon adding statements that's fine but I'd prefer just 1.
    Tuesday, April 21, 2015 1:24 PM

Answers

  • >>How would I go about adding multiple rectangles using the same lines of code?

    You could create a method that creates and returns x number of Rectangle elements:

            public IEnumerable<Rectangle> CreateRectangles(int numberOfRectsToCreate)
            {
                for (int i = 0; i < numberOfRectsToCreate; ++i)
                {
                    Rectangle rect = new Rectangle();
                    rect.Fill = Brushes.Blue;
                    rect.Width = 100;
                    rect.Height = 100;
                    yield return rect;
                }
            }

    ..and then call this method from anywhere in your code:

                IEnumerable<Rectangle> rects = CreateRectangles(5);
                foreach (Rectangle rect in rects)
                {
                    //add to StackPanel or do whatever with the Rectangle elements:
                    yourStackPanel.Children.Add(rect);
                }

    >>If I just have to create multiple polygon adding statements that's fine but I'd prefer just 1.

    When adding Point objects to a Polygon you can only add one per call to the Add method but you could call the Add method inside a loop, e.g:

                for(int i = 0; i < 10; ++i)
                {
                    //add to StackPanel or do whatever with the Rectangle elements:
                    Polygon p = new Polygon();
                    p.Points.Add(new Point());
                    
                }


    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't post several questions in the same thread.

    • Proposed as answer by Barry Wang Wednesday, April 22, 2015 8:00 AM
    • Marked as answer by Barry Wang Wednesday, April 29, 2015 8:17 AM
    Tuesday, April 21, 2015 1:46 PM
  • The best thing you could do here is to add the Point objects to a collection and then iterate through this collection:

    if (picToggle == 2)
                {
                    point3.X = point2.X;
                    point4.X = point5.X;
                    point7.X = point6.X;
                    point8.X = point1.X;
                    point4.Y = wire5.Y;
                    point7.Y = wire6.Y;
                    point3.Y = wire5.Y;
                    point8.Y = wire6.Y;
                    PointCollection blop = new PointCollection();
                    List<Point> thePoints = new List<Point> { point1, point2, point3, point4, point5, point6, point7, point8 };
                    foreach (var point in thePoints)
                        blop.Add(point);
                    plop.Stroke = new SolidColorBrush(Colors.Black);
                    plop.Fill = new SolidColorBrush(Colors.Yellow);
                    plop.Points = blop;
                    WirePlace.Children.Add(plop);
                    LastImageClicked = null;
                }

    It will save you a few lines of codes if that's what you want.


    Please remember to close the thread by marking helpful posts as answer and then start a new thread if you have a new question.

    • Marked as answer by Barry Wang Wednesday, April 29, 2015 8:17 AM
    Monday, April 27, 2015 8:58 PM

All replies

  • >>How would I go about adding multiple rectangles using the same lines of code?

    You could create a method that creates and returns x number of Rectangle elements:

            public IEnumerable<Rectangle> CreateRectangles(int numberOfRectsToCreate)
            {
                for (int i = 0; i < numberOfRectsToCreate; ++i)
                {
                    Rectangle rect = new Rectangle();
                    rect.Fill = Brushes.Blue;
                    rect.Width = 100;
                    rect.Height = 100;
                    yield return rect;
                }
            }

    ..and then call this method from anywhere in your code:

                IEnumerable<Rectangle> rects = CreateRectangles(5);
                foreach (Rectangle rect in rects)
                {
                    //add to StackPanel or do whatever with the Rectangle elements:
                    yourStackPanel.Children.Add(rect);
                }

    >>If I just have to create multiple polygon adding statements that's fine but I'd prefer just 1.

    When adding Point objects to a Polygon you can only add one per call to the Add method but you could call the Add method inside a loop, e.g:

                for(int i = 0; i < 10; ++i)
                {
                    //add to StackPanel or do whatever with the Rectangle elements:
                    Polygon p = new Polygon();
                    p.Points.Add(new Point());
                    
                }


    Please remember to close your threads by marking helpful posts as answer and then start a new thread if you have a new question. Please don't post several questions in the same thread.

    • Proposed as answer by Barry Wang Wednesday, April 22, 2015 8:00 AM
    • Marked as answer by Barry Wang Wednesday, April 29, 2015 8:17 AM
    Tuesday, April 21, 2015 1:46 PM
  • So how could I take this code and put in a loop that only loops once per call?

    if (a == 6 && LastImageClicked != null)
                {
                    if (picToggle == 2)
                    {
                        point3.X = point2.X;
                        point4.X = point5.X;
                        point7.X = point6.X;
                        point8.X = point1.X;
                        point4.Y = wire5.Y;
                        point7.Y = wire6.Y;
                        point3.Y = wire5.Y;
                        point8.Y = wire6.Y;
                        PointCollection blop = new PointCollection();
                        blop.Add(point1);
                        blop.Add(point2);
                        blop.Add(point3);
                        blop.Add(point4);
                        blop.Add(point5);
                        blop.Add(point6);
                        blop.Add(point7);
                        blop.Add(point8);
                        plop.Stroke = new SolidColorBrush(Colors.Black);
                        plop.Fill = new SolidColorBrush(Colors.Yellow);
                        plop.Points = blop;
                        WirePlace.Children.Add(plop);
                        LastImageClicked = null;
                    }
                }

    Monday, April 27, 2015 5:58 PM
  • "

    So how could I take this code and put in a loop that only loops once per call?

    "

    That's a trick question, right?

    Because it's already going to only run once.


    Hope that helps.

    Technet articles: Uneventful MVVM; All my Technet Articles

    Monday, April 27, 2015 6:05 PM
  • The best thing you could do here is to add the Point objects to a collection and then iterate through this collection:

    if (picToggle == 2)
                {
                    point3.X = point2.X;
                    point4.X = point5.X;
                    point7.X = point6.X;
                    point8.X = point1.X;
                    point4.Y = wire5.Y;
                    point7.Y = wire6.Y;
                    point3.Y = wire5.Y;
                    point8.Y = wire6.Y;
                    PointCollection blop = new PointCollection();
                    List<Point> thePoints = new List<Point> { point1, point2, point3, point4, point5, point6, point7, point8 };
                    foreach (var point in thePoints)
                        blop.Add(point);
                    plop.Stroke = new SolidColorBrush(Colors.Black);
                    plop.Fill = new SolidColorBrush(Colors.Yellow);
                    plop.Points = blop;
                    WirePlace.Children.Add(plop);
                    LastImageClicked = null;
                }

    It will save you a few lines of codes if that's what you want.


    Please remember to close the thread by marking helpful posts as answer and then start a new thread if you have a new question.

    • Marked as answer by Barry Wang Wednesday, April 29, 2015 8:17 AM
    Monday, April 27, 2015 8:58 PM
  • Yes. However I need it to run through this as many times as i need, and always create a new polygon and keeping the old one.
    Friday, May 1, 2015 1:55 PM