2010-11-18 56 views
1

因此,對於編碼項目,我需要創建一個「黑白棋」遊戲。現在我正在嘗試自己做大部分工作。基本上試圖重新學習C#。我只是想創建一個董事會。現在我使用的是文本框,B代表黑色,W代表白色。在課堂上使用文本框

我的問題是試圖創建我的板類。

我在這裏的代碼:

private TextBox[,] textboxes; 
public board() 
{ 
    textboxes = new TextBox[,] { 
     {textBox1,textBox2,textBox3,textBox4,textBox5,textBox6,textBox7,textBox8}, 
     {textBox11,textBox12,textBox13,textBox14,textBox15,textBox16,textBox17,textBox18}, 
     {textBox21,textBox22,textBox23,textBox24,textBox25,textBox26,textBox27,textBox28}, 
     {textBox31,textBox32,textBox33,textBox34,textBox35,textBox36,textBox37,textBox38}, 
     {textBox41,textBox42,textBox43,textBox44,textBox45,textBox46,textBox47,textBox48}, 
     {textBox51,textBox52,textBox53,textBox54,textBox55,textBox56,textBox57,textBox58}, 
     {textBox61,textBox62,textBox63,textBox64,textBox65,textBox66,textBox67,textBox68}, 
     {textBox71,textBox72,textBox73,textBox74,textBox75,textBox76,textBox77,textBox78}}; 
} 

這將創建一個箱子8X8格。我在一個名爲板子的課上有這個。它不會讓我在這裏使用這些文本框。

我得到這個錯誤:錯誤1無法訪問外型「WindowsFormsApplication1.Form1」通過嵌套類型「WindowsFormsApplication1.Form1.board」

任何想法或想法如何使這個的非靜態成員那麼我更容易做到這一點?

回答

1

顯然書寫的文本框一樣,是不是一個好的選擇=)

的選擇是:

  • 創建PictureBox和處理其Paint事件,並用它繪製.NET的強大Graphics對象;你會得到很酷的方法,如DrawRectangle()DrawEllipse()。示例代碼片段:

    int GridHeight = 50, GridWidth = 50; 
    // draw the graph grid     
    for (int i = 0; i < 8; i++) 
        for (int j = 0; j < 8; j++) 
         g.DrawRectangle(Pens.Black, i * GridWidth, j * GridHeight, GridWidth, GridHeight); 
    

    這樣可以輕鬆修改更高級別的電路板尺寸。

  • 如果必須使用文本框去,這可能是更容易實現與,則需要以上述相同的nested- for的方式動態地創建它們,雖然你動態創建TextBox ES,如互動:

    for (int i = 0; i < 8; i++) 
        for (int j = 0; j < 8; j++) 
        { 
         TextBox cell = new TextBox(); 
         cell.Top = i * GridHeight; 
         cell.Left = j * GridWidth; 
         cell.Click += new EventHandler(Cell_Click); 
         AllCells.Add(cell); 
        } 
    

    並相應地處理Cell_Click事件。

+0

Is AllCells是您定義的列表嗎?在你的第一個例子中也是g這些Graphics對象之一嗎?對不起,在這裏學習新東西。 – 2010-11-18 05:04:07

+0

@Lewis刀具IIi;據推測,是的。 – BeemerGuy 2010-11-18 05:06:01

+0

請問這個清單好像是列表 AllCell = new List (); – 2010-11-18 05:11:09

1

我不認爲使用文本框是一個好主意。但我缺乏這種經驗。 所以....我只能給你一些程序上的建議。

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
     InitializeComponent(); 
    } 

    class Board 
    { 
     private TextBox[,] textboxes; 

     public Board(Form1 form) 
     { 
      textboxes = new TextBox[,] 
      { 
       {form.textBox1, form.textBox2, ....} 
      }; 
     } 
    } 
}