2017-04-10 15 views
4

を分割します。C#の広場は、私は純粋な好奇心から、これをやっていると私は日のためにこれをやろうとしてきたが、私は立ち往生しているように見える

私は、次のやろうとしている

:一瞬

For this I'm using buttons and the "click" event

を、私はちょうど広場の一つが無限に私はそれをクリックするたびに分割するために取得しようとしています。 ここで私が働いているコードです:

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); 
      //topRightSquare.Click += new EventHandler(topRightSquare_Click); 
      //bottomLeftSquare.Click += new EventHandler(bottomLeftSquare_Click); 
      //bottomRightSquare.Click += new EventHandler(bottomRightSquare_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); 
      //topRightSquare.Click += new EventHandler(topRightSquare_Click); 
      //bottomLeftSquare.Click += new EventHandler(bottomLeftSquare_Click); 
      //bottomRightSquare.Click += new EventHandler(bottomRightSquare_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); 
     } 
    } 
} 

しかし、私はすべてのボタンが意図した動作である(より小さく、小さくなりますが、それが唯一のものだった代わりに、それは分割されません四角をクリックするたびに1平方メートルのためにそれは4にまた薄切りにされることが期待された)。

ご協力いただければ幸いです。

****************************** EDIT *************** ***************問題を解決しニーニョスの答えは、へ

感謝します。 私はいくつかの掃除を行ったので、解決策を探している人にとっての主な結果がここにあります。

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); 
     } 
    } 
} 
+3

あなたはより多くの正方形、毎回を持って覚えておく必要があります。

は見てみましょう。私が使用してみましたしかし、なぜあなたのコードは、既存の四角形を変更している..あなたは私が作成したオブジェクトの最小数でこれを行うにしようとしていた、気づいたあなたが分割し、正方形を削除し、そして4つの新しいもの – BugFinder

+0

のTiを交換する必要があり、それはです他の四角。私はあなたのヒントに従う必要があると思う、すべての正方形クリックして私はn + 3の正方形を作成し、元のクリックして処分する必要があります。 – Floaterz

+0

さてあなたはちょうどあなたが始まっ1を縮小し、これは非常にいいです – BugFinder

答えて

1

ここでは、この上の私の感想だ...

はまず、私は、square_Clickメソッドにボタンの宣言を削除役に立てば幸いフォーム作成時にそれが既存のコードの最大の問題でした。

第2に、すべてのボタンをクリックすると同じコードが実行されるため、2つの方法(square_ClickおよびtopLeftSquare_Click)を1つの方法にマージしました。

public partial class Form1 : Form 
{ 
    private Button centerSquare = new Button(); 

    public Form1() 
    { 
     InitializeComponent(); 
    } 

    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; 
     topLeftSquare.Click += new EventHandler(square_Click); 
     topRightSquare.Click += new EventHandler(square_Click); 
     bottomLeftSquare.Click += new EventHandler(square_Click); 
     bottomRightSquare.Click += new EventHandler(square_Click); 

     int newSquareHeight = senderSquare.Height/2; 
     int newSquareWidth = senderSquare.Width/2; 

     topLeftSquare.Size = new System.Drawing.Size(newSquareHeight, newSquareWidth); 
     topRightSquare.Size = new System.Drawing.Size(newSquareHeight, newSquareWidth); 
     bottomLeftSquare.Size = new System.Drawing.Size(newSquareHeight, newSquareWidth); 
     bottomRightSquare.Size = new System.Drawing.Size(newSquareHeight, newSquareWidth); 

     topLeftSquare.Location = new Point(senderSquare.Left, senderSquare.Top); 
     topRightSquare.Location = new Point(senderSquare.Left + newSquareHeight, senderSquare.Top); 
     bottomLeftSquare.Location = new Point(senderSquare.Left, senderSquare.Top + newSquareWidth); 
     bottomRightSquare.Location = new Point(senderSquare.Left + newSquareHeight, senderSquare.Top + newSquareWidth); 

     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); 

     this.Controls.Remove(senderSquare); 
    } 

    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); 
    } 
} 
+0

3以上を追加し、N + 3を作成する必要がいけません。 'Button senderSquare = sender as Button; 私はそれを理解しようとしていたので、ボタンを使用して属性を取得することができました。 非常に素晴らしいです。 – Floaterz

0

四角形の階層が必要です。以下の変更を参照してください。私はコードをデバッグしませんでしたが、問題を解決するための基本的な解決策を提供しました。

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 

namespace divideSquare 
{ 
    public partial class Form1 : Form 
    { 
     public ChildSquare root = new ChildSquare(); 
     public Form1() 
     { 
      InitializeComponent(); 
      this.Load += new EventHandler(Form1_Load); 
     } 

     private void square_Click(object sender, EventArgs e) 
     { 
      ChildSquare oldButton = sender as ChildSquare; 
      oldButton.topLeftSquare.Click += new EventHandler(topLeftSquare_Click); 
      //topRightSquare.Click += new EventHandler(topRightSquare_Click); 
      //bottomLeftSquare.Click += new EventHandler(bottomLeftSquare_Click); 
      //bottomRightSquare.Click += new EventHandler(bottomRightSquare_Click); 

      oldButton.topLeftSquare.Size = new System.Drawing.Size(oldButton.centerSquare.Height/2, oldButton.centerSquare.Width/2); 
      oldButton.topRightSquare.Size = new System.Drawing.Size(oldButton.centerSquare.Height/2, oldButton.centerSquare.Width/2); 
      oldButton.bottomLeftSquare.Size = new System.Drawing.Size(oldButton.centerSquare.Height/2, oldButton.centerSquare.Width/2); 
      oldButton.bottomRightSquare.Size = new System.Drawing.Size(oldButton.centerSquare.Height/2, oldButton.centerSquare.Width/2); 

      oldButton.topLeftSquare.Location = new Point(0, 0); 
      oldButton.topRightSquare.Location = new Point(50, 0); 
      oldButton.bottomLeftSquare.Location = new Point(0, 50); 
      oldButton.bottomRightSquare.Location = new Point(50, 50); 

      oldButton.topLeftSquare.BackColor = Color.Red; 
      oldButton.topRightSquare.BackColor = Color.Red; 
      oldButton.bottomLeftSquare.BackColor = Color.Red; 
      oldButton.bottomRightSquare.BackColor = Color.Red; 

      this.Controls.Add(oldButton.topLeftSquare); 
      this.Controls.Add(oldButton.topRightSquare); 
      this.Controls.Add(oldButton.bottomLeftSquare); 
      this.Controls.Add(oldButton.bottomRightSquare); 

      oldButton.centerSquare.Dispose(); 
     } 

     private void topLeftSquare_Click(object sender, EventArgs e) 
     { 
      ChildSquare childTopLeftSquare = sender as ChildSquare; 
      childTopLeftSquare.topLeftSquare.Click += new EventHandler(topLeftSquare_Click); 
      //topRightSquare.Click += new EventHandler(topRightSquare_Click); 
      //bottomLeftSquare.Click += new EventHandler(bottomLeftSquare_Click); 
      //bottomRightSquare.Click += new EventHandler(bottomRightSquare_Click); 

      childTopLeftSquare.topLeftSquare.Size = new System.Drawing.Size(childTopLeftSquare.topLeftSquare.Height/2, childTopLeftSquare.topLeftSquare.Width/2); 
      childTopLeftSquare.topRightSquare.Size = new System.Drawing.Size(childTopLeftSquare.topLeftSquare.Height/2, childTopLeftSquare.topLeftSquare.Width/2); 
      childTopLeftSquare.bottomLeftSquare.Size = new System.Drawing.Size(childTopLeftSquare.topLeftSquare.Height/2, childTopLeftSquare.topLeftSquare.Width/2); 
      childTopLeftSquare.bottomRightSquare.Size = new System.Drawing.Size(childTopLeftSquare.topLeftSquare.Height/2, childTopLeftSquare.topLeftSquare.Width/2); 

      childTopLeftSquare.topLeftSquare.Location = new Point(0, 0); 
      childTopLeftSquare.topRightSquare.Location = new Point(10, 0); 
      childTopLeftSquare.bottomLeftSquare.Location = new Point(0, 10); 
      childTopLeftSquare.bottomRightSquare.Location = new Point(10, 10); 

      childTopLeftSquare.topLeftSquare.BackColor = Color.Red; 
      childTopLeftSquare.topRightSquare.BackColor = Color.Red; 
      childTopLeftSquare.bottomLeftSquare.BackColor = Color.Red; 
      childTopLeftSquare.bottomRightSquare.BackColor = Color.Red; 

      childTopLeftSquare.Controls.Add(childTopLeftSquare.topLeftSquare); 
      childTopLeftSquare.Controls.Add(childTopLeftSquare.topRightSquare); 
      childTopLeftSquare.Controls.Add(childTopLeftSquare.bottomLeftSquare); 
      childTopLeftSquare.Controls.Add(childTopLeftSquare.bottomRightSquare); 

     } 

     private void Form1_Load(object sender, EventArgs e) 
     { 
      root.Click += new EventHandler(square_Click); 
      root.Size = new System.Drawing.Size(50, 50); 
      root.BackColor = Color.Red; 
      this.Controls.Add(root); 
     } 
    } 
    public class ChildSquare : Button 
    { 
     public Button centerSquare = new Button(); 
     public Button topLeftSquare = new Button(); 
     public Button topRightSquare = new Button(); 
     public Button bottomLeftSquare = new Button(); 
     public Button bottomRightSquare = new Button(); 

    } 
} 
1

私はちょうどあなたのコードを試して、いくつか修正しました。以下のコードを使用してください、それはあなたが必要な方法で働いた。コードは自明です。任意のコード行について助けが必要な場合は、私に知らせてください。私はそれはあなたが新しいボタンを作成する必要が毎回、とあなたのコードだけでインスタンス化されたボタンを再利用しますので、あなたが

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) 
     { 
      Split(sender as Button); 
      centerSquare.Dispose(); 
     } 

     private void topLeftSquare_Click(object sender, EventArgs e) 
     { 
      Split(sender as Button); 
     } 

     private void Split(Button source) 
     { 
      Button topRightSquare = new Button(); 
      Button bottomLeftSquare = new Button(); 
      Button bottomRightSquare = new Button(); 

      topLeftSquare.Click += new EventHandler(topLeftSquare_Click); 

      int width = source.Height/2; 

      topLeftSquare.Size = new System.Drawing.Size(width,width); 
      topRightSquare.Size = new System.Drawing.Size(width, width); 
      bottomLeftSquare.Size = new System.Drawing.Size(width, width); 
      bottomRightSquare.Size = new System.Drawing.Size(width, width); 

      topLeftSquare.Location = new Point(0, 0); 
      topRightSquare.Location = new Point(topLeftSquare.Width, 0); 
      bottomLeftSquare.Location = new Point(0, topLeftSquare.Height); 
      bottomRightSquare.Location = new Point(topLeftSquare.Width , topLeftSquare.Height); 

      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(400, 400); 
      centerSquare.BackColor = Color.Red; 
      this.Controls.Add(centerSquare); 
     } 

    } 
関連する問題