Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4542706
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:25:44+00:00 2026-05-21T15:25:44+00:00

So, I am trying to emulate the minesweeper game in winforms, just as an

  • 0

So, I am trying to emulate the minesweeper game in winforms, just as an exercise. So far I have two classes, one called “Cell” which derives from regular button class, but has few properties of its own and a class that handles the logic (basically I made a two-dimensional array filled with objects of type “Cell” and populate it with bombs). I ran into a problem – how do I attach my array of “Cell” button controls to the form? Without re-writing everything from scratch? Both classes are unfinished obviously, it’s just that I wanted to check how it would look on a form and realized that I am stuck.

Here is my Cell class

class Cell : Button
{
    //private GraphicsPath path;

    const int width = 20;
    const int height = 20;

    public byte Value { get; set; }
    public bool IsBomb { get; set; }

    public Cell()
    {

    }

    public Cell(int x, int y)
    {
        this.Location = new Point(x, y);
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        base.OnPaint(pevent);

        this.Width = width;
        this.Height = height;

    }

    protected override void OnClick(EventArgs e)
    {
        base.OnClick(e);
        this.Text = Value.ToString();

    }
}

And here is my array class

class CellArray
{
    private int _rows = 0;
    private int _columns = 0;
    private int _bombAmount = 0;
    private Random rand;
    private Cell[,] cellMatrix;

    public CellArray(int rows, int columns, int bombAmount)
    {
        _rows = rows;
        _columns = columns;
        _bombAmount = bombAmount;
        populate();
        setBombs();
    }

    private void populate()
    {
        cellMatrix = new Cell[_rows, _columns];

        for (int i = 1; i < _rows; i++)
        {
            for (int j = 1; j < _columns; j++)
            {
                cellMatrix[i, j] = new Cell();
                cellMatrix[i, j].IsBomb = false;

            }
        }
    }

    private void setBombs()
    {
        //*****************************************QUESTIONABLE************************************
        rand = new Random();
        int k = 1;
        while (k < _bombAmount)
        {
        Flag:
            {
                int i = rand.Next(_rows);
                int j = rand.Next(_columns);
                if (cellMatrix[i, j].IsBomb == false)
                    cellMatrix[i, j].IsBomb = true;
                else
                    goto Flag;
            }
        }
        //*****************************************QUESTIONABLE************************************

        for (int i = 1; i < _rows; i++)
        {
            for (int j = 1; j < _columns; j++)
            {
                if (cellMatrix[i - 1, j - 1].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i - 1, j].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i, j - 1].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i - 1, j + 1].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i, j + 1].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i + 1, j + 1].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i + 1, j].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
                if (cellMatrix[i + 1, j - 1].IsBomb == true)
                {
                    cellMatrix[i, j].Value++;
                }
            }
        }

    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:25:45+00:00Added an answer on May 21, 2026 at 3:25 pm

    how do I attach my array of “Cell”
    button controls to the form

    This issue can be solved similar to this one:

    Using programatically created CheckBoxes in Windows Form [C#]

    The idea is to use YourForm.Controls.Add(...). Don’t forget to provide proper locations for your cells/buttons within the forms coordinate system.

    Of course, I would like to mention that IMHO deriving Cell from Button is a horrible design decision. Better separate your data classes (like Cell) completely from the GUI classes (like Button) and choose a technique like the one Asher suggested in his first answer (add a cell to the Tag property of each Button) to create a connection between Cell objects and corresponding Button objects.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several UIImage subviews in one UIScrollView. I'm trying to emulate iPhones's Photos
I am trying to emulate a system of type classes in F#; I would
I am trying to emulate the size of a Galaxy Note screen, which is
I'm trying to emulate some pseudo-classes and attribute selectors in Internet Explorer 6 and
I am trying to emulate the Maps 'Share Location' via email functionality. I have
I have a link that i am trying to emulate a click on. I
I've been trying to emulate in C# a behavior which I was implementing without
I'm trying to emulate this graph: If I have a correlation matrix how can
I am trying to emulate some java.lang and java.io classes, e.g. OutputStream within GWT.
I'm trying to emulate the way an international keyboard works. If you use one

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.