zerioto smoke
zerioto smoke

Reputation: 33

C# find panels adjecent to panel being clicked

I'm trying to make a little game where you turn all panels green. I do this by getting a 5x5 grid of panels, every panel has a 1/3 chance to be green, otherwise it will start as red. my problem is that i do not have the slightest clue how to start the main problem. when i click a panel, the panel above, left ,right and below need to change color aswell. at the moment i do not know how to identify which panels are next to the one being clicked.

this is my code:

public partial class Form1 : Form
{
    Panel[,] PanelArray = new Panel[5,5];
    Random R = new Random();
    int R1;

    public Form1()
    {
        InitializeComponent();
        for (int r = 0; r < 5; r++)
        {
            for (int c = 0; c < 5; c++)
            {
                R1 = R.Next(0, 3);

                PanelArray[r, c] = new Panel
                {
                    Size = new Size(50, 50),
                    Location = new Point(PanelContainer.Width / 5 * c, PanelContainer.Height / 5 * r),
                    BackColor = Color.Red,
                    BorderStyle = BorderStyle.Fixed3D
                };
                PanelArray[r, c].Click += new EventHandler(PanelArray_Click);
                if (R1 == 1) PanelArray[r, c].BackColor = Color.Green;
                PanelContainer.Controls.Add(PanelArray[r, c]);
            }
        }
    }

    private void PanelArray_Click(object sender, EventArgs e)
    {
        Panel P = (Panel)sender;

        if (P.BackColor == Color.Red) P.BackColor = Color.Green;
        else if (P.BackColor == Color.Green) P.BackColor = Color.Red;

        if (CheckWin()) MessageBox.Show("test");
    }

    private bool CheckWin()
    {
        //foreach panel green blah blah
        return false;
    }
}

}`

Upvotes: 0

Views: 93

Answers (2)

jdweng
jdweng

Reputation: 34421

Here is easy trick to get row and column. Create a class that inherits the Panel and adds a row and column. See code below

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
    {
        MyPanel[,] PanelArray = new MyPanel[5, 5];
        Random R = new Random();
        int R1;

        public Form1()
        {
            InitializeComponent();
            for (int r = 0; r < 5; r++)
            {
                for (int c = 0; c < 5; c++)
                {
                    R1 = R.Next(0, 3);

                    PanelArray[r, c] = new MyPanel
                    {
                        Size = new Size(50, 50),
                        Location = new Point(PanelContainer.Width / 5 * c, PanelContainer.Height / 5 * r),
                        BackColor = Color.Red,
                        BorderStyle = BorderStyle.Fixed3D,
                        row = r,
                        col = c
                    };
                    PanelArray[r, c].Click += new EventHandler(PanelArray_Click);
                    if (R1 == 1) PanelArray[r, c].BackColor = Color.Green;
                    PanelContainer.Controls.Add(PanelArray[r, c]);
                }
            }
        }

        private void PanelArray_Click(object sender, EventArgs e)
        {
            MyPanel P = sender as MyPanel;
            int row = P.row;
            int col = P.col;

            if (P.BackColor == Color.Red) P.BackColor = Color.Green;
            else if (P.BackColor == Color.Green) P.BackColor = Color.Red;

            if (CheckWin()) MessageBox.Show("test");
        }

        private bool CheckWin()
        {
            //foreach panel green blah blah
            return false;
        }
    }
    public class MyPanel : Panel
    {
        public int row { get; set; }
        public int col { get; set; }
    }
}

Upvotes: 0

Tor
Tor

Reputation: 631

You can use the Tag property in your Panel objects to store some information.

PanelArray[r, c] = new Panel
{
    Size = new Size(50, 50),
    Location = new Point(PanelContainer.Width / 5 * c, PanelContainer.Height / 5 * r),
    BackColor = Color.Red,
    BorderStyle = BorderStyle.Fixed3D,
    Tag = (Row: r, Column: c)
};   

In your PanelArray_Click method, you can get the indexes:

var indexes = ((int Row, int Column))P.Tag;
var row = indexes.Row;
var column = indexes.Column;
// Todo: your logic here

In the Tag property, you can store any object, so you can create some class to store data, if you need.

Other solution is two for loops, to get the indexes, like:

private (int Row, int Column) GetIndexes(Panel panel)
{
    for (int x = 0; x < PanelArray.GetLength(0); x++)
    {
        for (int y = 0; y < PanelArray.GetLength(1); y++)
        {
            if (PanelArray[x, y] == panel)
            {
                return (x, y);
            }
        }
    }

    throw new Exception("Not found.");
}

And then you can use in your PanelArray_Click method:

var indexes = this.GetIndexes(P);
var row = indexes.Row;
var column = indexes.Column;
// Todo: your logic here

Upvotes: 1

Related Questions