2016-04-17 10 views
0

私はC#のチェスゲームを作成中です。 Java-Swing環境から来て、フィールド8x8を作成して基本属性を与える標準関数を作成しました。 ( - 「E3の騎士」あなたのような動きを入力できるように)テーブルレイアウトにコンポーネントを追加(作成および追加)する方法は?

 Board = new Label[8, 8]; 
      for (int i = 0; i < 8; i++) 
      { 

       for (int j = 0; j < 8; j++) 
       { 
        Board[i, j] = new System.Windows.Forms.Label(); 
        Board[i, j].Location = new Point(i * 50, j * 50); 
        Board[i, j].Size = new System.Drawing.Size(50, 50); 
        Board[i, j].Visible = true; 


        if ((i + j) % 2 == 0) // Color decision 
        { 
         Board[i, j].BackColor = Color.Black; 
        } 

        else { 
         Board[i, j].BackColor = Color.White; 
        } 

        this.Controls.Add(Board[i, j]); 
       } 
      } 

は今チェスボードの外縁のABCのと123のを保持している2つの追加の配列があります。

私はすべてのコンポーネントを画面に追加することができましたが、現在は重複しています。私は9x9 "grid-layout"を作成し、すべてのコンポーネントを追加することを考えていました。

Javaから私のような簡単なコマンドを使用しています:

GridLayout gl = new Gridlayout(3,3); 
    this.setLayout(gl); 

そして追加されたすべての要素が自動的にグリッドに入れます。 何時間もの研究の末、C#には何も見つかりません。 TableLayoutで遊ぶことは、その解決策より多くの問題を引き起こします。

私の質問は(gridlayoutを実装し、すべてのラベルを追加する方法です。 私はレイアウトコードを投稿していないことをアドバンテージで申し訳なく思っていますが、私はそれがちょうど混乱であり、何もしないと言っていたようです。

ありがとうございました:)

答えて

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 WindowsFormsApplication1 
{ 
    public partial class Form1 : Form 
    { 

     public Form1() 
     { 
      InitializeComponent(); 

      MyButton myButton = new MyButton(this); 
     } 

    } 
    public class MyButton : Button 
    { 
     public static List<List<MyButton>> board { get; set; } 
     public static List<MyButton> buttons { get; set; } 
     const int WIDTH = 10; 
     const int HEIGTH = 10; 
     const int SPACE = 5; 
     const int ROWS = 10; 
     const int COLS = 20; 
     public int row { get; set; } 
     public int col { get; set; } 

     public MyButton() 
     { 
     } 
     public MyButton(Form1 form1) 
     { 
      board = new List<List<MyButton>>(); 
      buttons = new List<MyButton>(); 

      for (int _row = 0; _row < ROWS; _row++) 
      { 
       List<MyButton> newRow = new List<MyButton>(); 
       board.Add(newRow); 
       for (int _col = 0; _col < COLS; _col++) 
       { 
        MyButton newButton = new MyButton(); 
        newButton.row = _row; 
        newButton.col = _col; 
        newButton.Width = WIDTH; 
        newButton.Height = HEIGTH; 
        newButton.Top = _row * (HEIGTH + SPACE); 
        newButton.Left = _col * (WIDTH + SPACE); 
        form1.Controls.Add(newButton); 
        newRow.Add(newButton); 
        buttons.Add(newButton); 
       } 
      } 
     } 

    } 
} 
+0

あなたと同じようなシステムを使用してくれてありがとう、それは完璧に機能します=) –

0

を私は勝利に推測することは少し異なる動作しますフォーム。 TableLayoutPanelを作成し、TableLayoutPanel.Controlsにアクセスし、Control.ControlCollection.Addメソッドを呼び出すことで、新しいコントロールを1つずつ追加する必要があります。

var panel = new TableLayoutPanel(); 
panel.ColumnCount = 3; 
panel.RowCount = 4; 

for(int i = 0; i< panel.ColumnCount; ++i) 
    panel.ColumnStyles.Add(new System.Windows.Forms.ColumnStyle(System.Windows.Forms.SizeType.Percent, 50F)); 

for (int i = 0; i < panel.RowCount; ++i) 
    panel.RowStyles.Add(new System.Windows.Forms.RowStyle(System.Windows.Forms.SizeType.Percent, 50F)); 

panel.Dock = DockStyle.Fill; 
this.Controls.Add(panel); 

for (int c = 0; c < 3; ++c) 
{ 
    for (int r = 0; r < 4; ++r) 
    { 
     var btn = new Button(); 
     btn.Text = (c+r).ToString(); 
     btn.Dock = DockStyle.Fill; 
     panel.Controls.Add(btn, c, r); 
    } 
} 
関連する問題