I do this out of sheer curiosity, and I tried to do it for several days, but I seem to be stuck.
I am trying to do the following:

for now, I'm trying to just get one of the squares for infinite separation every time I click on it. Here is the code I'm working on:
using System;
using System.Drawing;
using System.Windows.Forms;
namespace divideSquare
{
public partial class Form1 : Form
{
private Button centerSquare = new Button();
private Button topLeftSquare = new Button();
private Button topRightSquare = new Button();
private Button bottomLeftSquare = new Button();
private Button bottomRightSquare = new Button();
public Form1()
{
InitializeComponent();
}
private void square_Click( object sender, EventArgs e )
{
topLeftSquare.Click += new EventHandler( topLeftSquare_Click );
topLeftSquare.Size = new System.Drawing.Size( centerSquare.Height / 2, centerSquare.Width / 2 );
topRightSquare.Size = new System.Drawing.Size( centerSquare.Height / 2, centerSquare.Width / 2 );
bottomLeftSquare.Size = new System.Drawing.Size( centerSquare.Height / 2, centerSquare.Width / 2 );
bottomRightSquare.Size = new System.Drawing.Size( centerSquare.Height / 2, centerSquare.Width / 2 );
topLeftSquare.Location = new Point( 0, 0 );
topRightSquare.Location = new Point( 50, 0 );
bottomLeftSquare.Location = new Point( 0, 50 );
bottomRightSquare.Location = new Point( 50, 50 );
topLeftSquare.BackColor = Color.Red;
topRightSquare.BackColor = Color.Red;
bottomLeftSquare.BackColor = Color.Red;
bottomRightSquare.BackColor = Color.Red;
this.Controls.Add( topLeftSquare );
this.Controls.Add( topRightSquare );
this.Controls.Add( bottomLeftSquare );
this.Controls.Add( bottomRightSquare );
centerSquare.Dispose();
}
private void topLeftSquare_Click( object sender, EventArgs e )
{
topLeftSquare.Click += new EventHandler( topLeftSquare_Click );
topLeftSquare.Size = new System.Drawing.Size( topLeftSquare.Height / 2, topLeftSquare.Width / 2 );
topRightSquare.Size = new System.Drawing.Size( topLeftSquare.Height / 2, topLeftSquare.Width / 2 );
bottomLeftSquare.Size = new System.Drawing.Size( topLeftSquare.Height / 2, topLeftSquare.Width / 2 );
bottomRightSquare.Size = new System.Drawing.Size( topLeftSquare.Height / 2, topLeftSquare.Width / 2 );
topLeftSquare.Location = new Point( 0, 0 );
topRightSquare.Location = new Point( 10, 0 );
bottomLeftSquare.Location = new Point( 0, 10 );
bottomRightSquare.Location = new Point( 10, 10 );
topLeftSquare.BackColor = Color.Red;
topRightSquare.BackColor = Color.Red;
bottomLeftSquare.BackColor = Color.Red;
bottomRightSquare.BackColor = Color.Red;
this.Controls.Add( topLeftSquare );
this.Controls.Add( topRightSquare );
this.Controls.Add( bottomLeftSquare );
this.Controls.Add( bottomRightSquare );
}
private void Form1_Load( object sender, EventArgs e )
{
centerSquare.Click += new EventHandler( square_Click );
centerSquare.Size = new System.Drawing.Size( 50, 50 );
centerSquare.BackColor = Color.Red;
this.Controls.Add( centerSquare );
}
}
}
But every time I click on a square, it does not split, instead all the buttons become smaller and smaller (this is the expected behavior, but it is intended for only one square, and it is expected that it will be cut into 4).
Any help would be greatly appreciated.
******************************** EDIT ***************** ***********
Thank you so much Ninos to answer that I solved the problem. I did some cleaning, so here is the main result for those who are also looking for a solution.
using System;
using System.Drawing;
using System.Windows.Forms;
namespace divideSquare
{
public partial class Form1 : Form
{
private Random rnd = new Random();
private int _initHeight = 500;
private int _initWidth = 500;
private Button centerSquare = new Button();
public Form1()
{
InitializeComponent();
}
private void setSquareLocation( Button[] squareArray, Button senderSquare, int newHeight, int newWidth )
{
squareArray[ 0 ].Location = new Point( senderSquare.Left, senderSquare.Top );
squareArray[ 1 ].Location = new Point( senderSquare.Left + newHeight, senderSquare.Top );
squareArray[ 2 ].Location = new Point( senderSquare.Left, senderSquare.Top + newWidth );
squareArray[ 3 ].Location = new Point( senderSquare.Left + newHeight, senderSquare.Top + newWidth );
}
private void square_Click( object sender, EventArgs e )
{
Button topLeftSquare = new Button();
Button topRightSquare = new Button();
Button bottomLeftSquare = new Button();
Button bottomRightSquare = new Button();
Button senderSquare = sender as Button;
Button[] squareArray = { topLeftSquare, topRightSquare, bottomLeftSquare, bottomRightSquare };
int newSquareHeight = senderSquare.Height / 2;
int newSquareWidth = senderSquare.Width / 2;
foreach (var square in squareArray ) {
square.Click += new EventHandler( square_Click );
square.Size = new Size( newSquareWidth, newSquareHeight );
square.BackColor = Color.FromArgb( rnd.Next( 256 ), rnd.Next( 256 ), rnd.Next( 256 ) );
}
setSquareLocation( squareArray, senderSquare, newSquareHeight, newSquareWidth );
foreach ( var square in squareArray ) {
this.Controls.Add( square );
}
this.Controls.Remove( senderSquare );
}
private void Form1_Load( object sender, EventArgs e )
{
this.Width = _initWidth + 18;
this.Height = _initHeight + 40;
this.MaximumSize = new Size(_initWidth + 18, _initHeight + 40 );
this.MinimumSize = new Size( _initWidth + 18, _initHeight + 40 );
centerSquare.Click += new EventHandler( square_Click );
centerSquare.Size = new System.Drawing.Size(_initWidth, _initHeight );
centerSquare.BackColor = Color.FromArgb( rnd.Next( 256 ), rnd.Next( 256 ), rnd.Next( 256 ) );
this.Controls.Add( centerSquare );
}
}
}