1

So for a coding project I need to create an Othello game. Now I'm trying to do most of it on my own. Basically trying to re-learn C#. I'm just trying to create a board. Now I am using textboxes and B represents black and W represents a white piece.

My problem is trying to create my board class.

My code here:

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

This creates an 8x8 grid of boxes. I have this in a class named board. It won't let me use these textboxes here.

I get this error: Error 1 Cannot access a non-static member of outer type 'WindowsFormsApplication1.Form1' via nested type 'WindowsFormsApplication1.Form1.board'

Any thoughts or ideas how to make this easier then I am doing it?

2 Answers 2

1

Obviously writing out the text boxes like that is not a good choice =)

The choices are:

  • Create a PictureBox and handle its Paint event and draw on it using .NET's powerful Graphics object; you get very cool methods such as DrawRectangle() and DrawEllipse(). Example snippet:

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

    That way you can easily modify the size of the board at a higher level.

  • If you must go with text boxes, which might be easier to implement interaction with, you need to create them dynamically in the same nested-for manner above, though you create TextBoxes on the fly, such as:

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

    And handle the Cell_Click event accordingly.

Sign up to request clarification or add additional context in comments.

3 Comments

Is AllCells a list you defined? also is g in you first example one of those Graphics objects? Sorry, learning new things here.
@Lewis Cutter IIi; presumably, yes.
Would this list lok like List<TextBox> AllCell = new List<TextBox>();
1

I Don't think using textbox is a good idea. But I'm lack of this kind of experience. So.... I can only give you some procedural advice.

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

    class Board
    {
        private TextBox[,] textboxes;

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.