Chargerfan 0 Newbie Poster

I have written code in wpf that when the user presses a button on the grid, several rectangles of equal size will be displayed and have equal margins around them. I have also written code that when the user resizes the window, the rectangles will grow as the user makes the window bigger and shrink as the user makes the window smaller, while still having equal spacing between them.

My problem is that when I run the program, the rectangles are automatically drawn. I noticed that when I comment out the code for the resize event, no rectangles are drawn, even when the button is pressed. Any ideas on why this is and how I might be able to fix it?

here are the event handlers for reference:

private void PaintRect()
        {
            rows = Convert.ToInt32(rowNum.Text);
            columns = Convert.ToInt32(colNum.Text);
            double canvasWidth = canvas1.ActualWidth;
            double canvasHeight = canvas1.ActualHeight;
            double rectWidth = canvasWidth / (2 * columns + 1);
            double rectHeight = canvasHeight / (2 * rows + 1);
            
            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < columns; j++)
                {
                    Rectangle rect = new Rectangle();
                    rect.Fill = brushColor;
                    rect.Width = rectWidth;
                    rect.Height = rectHeight;
                    rect.Margin = new Thickness(rectWidth + (rectWidth * 2 * j), rectHeight + (rectHeight * 2 * i), rectWidth + (rectWidth * 2 * j), rectHeight + (rectHeight * 2 * i));
                    canvas1.Children.Add(rect);
                }
            }
        }

        private void startButton_Click(object sender, RoutedEventArgs e)
        {
            PaintRect();
        }

        private void Window_SizeChanged(object sender, SizeChangedEventArgs e)
        {
            canvas1.Children.Clear();
            PaintRect();
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.