Ahmed Saad
Ahmed Saad

Reputation: 177

ArgumentException when setting the Height/Width of a rectangle in wpf

for (int x = 0; x < route.Length; x++ )
        {
            if (nodes[route[x] + 1, 0] == nodes[route[x], 0])    //right or left
            {
                path[x] = new Rectangle();
                path[x].Fill = new SolidColorBrush(Colors.Red);
                int width = nodes[route[x] + 1, 1] - nodes[route[x], 1];
                path[x].Width = width;
                path[x].Height = 10;
                Canvas.SetLeft(path[x], nodes[route[x], 0]);
                Canvas.SetTop(path[x], nodes[route[x], 1] - 10);
                MapCont.Children.Add(path[x]);
            }
            else                                                 //up or down
            {
                path[x] = new Rectangle();
                path[x].Fill = new SolidColorBrush(Colors.Red);
                int height = nodes[route[x] + 1, 0] - nodes[route[x], 0];
                path[x].Width = 10;
                path[x].Height = height;
                Canvas.SetLeft(path[x], nodes[route[x], 0]);
                Canvas.SetTop(path[x], nodes[route[x], 1] - 10);
                MapCont.Children.Add(path[x]);
            }

        }

path[x].Width gives the ArgumentException.

the code is supposed to take a list of coordinates and draws triangles between them.

Thanks in Advance

Upvotes: 0

Views: 461

Answers (1)

GreatJobBob
GreatJobBob

Reputation: 271

You will get an ArgumentException if you attempt to set the Width to a negative value. You may want to change line that set Width to use absolute value like below:

int width = Math.Abs(nodes[route[x] + 1, 1] - nodes[route[x], 1]);

Upvotes: 2

Related Questions