单击按钮并插入图像后如何禁用图片框

How to disable a picture box once I clicked a button and an image has been inserted

我在面板中创建了一个网格,并为一个 6x7 网格添加了 42 个图片框,每次单击该行的按钮时,它都会插入一个红色或黄色方格的图像。我试图让按钮在顶部的框中添加另一个图像的另一个图像。我创建了一个数组作为检查功能,以查看哪个玩家获胜。 我的代码是:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Connect4
{
    public partial class Form2 : Form
    {
        int i = 1;

        int[,] a = new int[7, 6];
        public Form2()
        {
            InitializeComponent();
        }
        private void tableLayoutPanel1_MouseClick(object sender, MouseEventArgs e)
        {

        }


        private void button1_Click(object sender, EventArgs e)
        {
            if (i %2 == 1)
            {
                pictureBox1.Image = Image.FromFile(@"C:\ Documents\Desktop\PROJECT FOR LAPTOP\red.png");
                pictureBox1.Enabled = false;
            }
            else
            {
                pictureBox1.Image = Image.FromFile(@"C:\ Documents\Desktop\PROJECT FOR LAPTOP=\yellow.png");
                pictureBox1.Enabled = false;
            }
            i++;

           
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {
            Graphics g = e.Graphics;
            int cols = 7;
            int rows = 6;
            int width = panel1.Width / cols;
            int height = panel1.Height / rows;
            for (int col = 1; col < cols; col++)
            {
                e.Graphics.DrawLine(Pens.Black, new Point(col * width, 0), new Point(col * width, panel1.Height));
            }
            for (int row = 1; row < rows; row++)
            {
                e.Graphics.DrawLine(Pens.Black, new Point(0, row * height), new Point(panel1.Width, row * height));
            }
        }

        private void Form2_Load(object sender, EventArgs e)
        {
            panel1.SizeChanged += Panel1_SizeChanged;
            panel1.Paint += panel1_Paint;
        }

        private void Panel1_SizeChanged(object sender, EventArgs e)
        {
            panel1.Invalidate();
        }
    }
}

到目前为止我的设计是这样的(https://i.stack.imgur.com/Rg8Vg.png) 单击按钮后红色变为黄色圆圈

我原以为它会先放入红色计数器图像,然后当我单击同一个按钮时,它会在其顶部的图片框中添加一个黄色计数器

这是从右上角到左下角的代码:

if (!winFound) //if win is not found on horizontal check Right Diagonals 
        {
            //Diagonal Upper right to bottom left(2D Array)
            for (int row = 3; row < 5 && !winFound; row++)
            {
                rowFound = row;
                for (int col = 0; col <= 3 && !winFound; col++)
                {
                    colFound = col;
                    if (board[row, col] != null)
                    {
                        temp = true;
                        winFound =
                            (board[row, col] == board[row + 1, col + 1]) &&
                            (board[row, col] == board[row + 2, col + 2]) &&
                            (board[row, col] == board[row + 3, col + 3]);
                    }
                }
            }
        }

我不确定为什么它不检查 win

假设左边第一列底部是pb1,右下是pb7,左边是button1,右边是button7对。

以下是您的操作方法,只需在底部设置一组按钮即可选择将下一块放入哪一列:

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

    private String[,] board = new String[6,7]; // six rows by seven columns 
    private int i = 1; // odd = red, even = yellow
    private List<Button> buttons;
    private Image red;// = Image.FromFile(@"C:\Users\ogisi\OneDrive - Wilmslow High School\Desktop\PROJECT FOR LAPTOP\red.png");
    private Image yellow;// = Image.FromFile(@"C:\Users\ogisi\OneDrive - Wilmslow High School\Desktop\PROJECT FOR LAPTOP\yellow.png");

    private void Form1_Load(object sender, EventArgs e)
    {
        // only ONE row of buttons, from Left to Right, BELOW all the PictureBoxes
        // PBs are placed from left to right, bottom to top
        // pb1 is at bottom left, pb2 to its right, pb3 to its right, etc...
        // pb8 in second row from the bottom on the left, pb9 to its right, pb10 to its right, etc...
        // pb15 in third row from bottom on the left, etc..
        // pb42 is in the top row in the rightmost column
        buttons = new List<Button> { button1, button2, button3, button4, button5, button6, button7 };
        foreach (Button btn in buttons)
        {
            btn.Click += Btn_Click;
        }
        nextTurnColor();
    }

    private void Btn_Click(object sender, EventArgs e)
    {
        Button btn = (Button)sender;
        int column = buttons.IndexOf(btn);

        int rowPlaced = -1;
        for(int row=0; row<6; row++)
        {
            if(board[row,column] == null)
            {
                rowPlaced = row;
                board[row, column] = (i % 2 == 1) ? "R" : "Y";

                String pbPrefix = "pictureBox"; // "pb"
                String ctlName = pbPrefix + ((row * 7) + (column + 1));
                PictureBox pb = this.Controls.Find(ctlName, true).FirstOrDefault() as PictureBox;
                pb.BackColor = (i % 2 == 1) ? Color.Red : Color.Yellow;
                //pb.Image = (i % 2 == 1) ? red : yellow;
                if (row == 5)
                {
                    btn.Enabled = false; // column is full!
                }

                i++;                    
                break;
            }
        }
        if (rowPlaced != -1)
        {                
            // ... check "board" for a win condition ...

            // if no win, then change player
            nextTurnColor();
        }
    }

    private void nextTurnColor()
    {
        this.Text = (i % 2 == 1) ? "Red's turn." : "Yellow's turn.";
    }

}

样本运行:(*未执行获胜条件检查!)

下面是检查水平获胜的示例。

仔细阅读这些评论!

// an empty spot in the board will be null
// red spot will contain "R"
// yellow spot will contain "Y"
private String[,] board = new String[6, 7];

private void FooBar()
{
    // here is how to check for a horizontal win
    // we need to check each row, this is the outer loop
    // we need to check the columns, thus the inner loop
    // for four in a row, horizontally, we canstart anywhere
    // from column 0 up to  to column 3, but cannot start
    // from column 4 or above as there wouldn't be enough
    // room to finish the row of four
    //
    // 0 1 2 3 4 5 6
    // x x x x
    //       x x x x        
    //
    // board.getLength(0) tells us how many rows in 2D array
    // board.getLength(1) tells us how many columns in 2D array
    int rowFound = -1;
    int colFound = -1;
    bool winFound = false; // assume no win until proven otherwise
    for (int row = 0; row < board.GetLength(0) && !winFound; row++)
    {
        rowFound = row;
        for (int col = 0; col <= (board.GetLength(1) - 4) && !winFound; col++)
        {
            colFound = col;
            // we want to ignore any starting position that is blank
            // so we don't end up getting a four in a row "match"
            // because all of them are equal, but contain null
            if (board[row, col] != null)
            {
                // are the next 3 horizontally the same as the first?
                // we don't need to worry about an index out of bounds
                // here since we controlled the inner loop above
                // with "(board.GetLength(1) - 4)"
                // both loops will drop out if winFound becomes true!
                winFound =
                    (board[row, col] == board[row, col + 1]) &&
                    (board[row, col] == board[row, col + 2]) &&
                    (board[row, col] == board[row, col + 3]);                        
            }
        }        
    }      

    if (!winFound) {
        // ... check for vertical win in here ...
    }

    if (!winFound) {
        // ... check for diagonal win (upper left to bottom right) in here ...
    }

    if (!winFound) {
        // ... check for diagonal win (upper right to bottom left) in here ...
    }

    // if four in a row was never found, winFound will still be false
    if (winFound)
    {
        // ... there was a winner! ...
        String winner = board[rowFound, colFound];
        Console.WriteLine("The winner was: " + winner);
    }
}

希望您能以这个例子为例,弄清楚如何将其应用于检查垂直 and/or 对角线获胜条件。不要忘记,在检查对角线获胜时,您需要同时检查左上角到右下角的对角线和右上角到左下角的对角线。

对于从左上角到右下角的检查,这张图片显示了可能的起始位置:

对于行,它们仅从第 3 行和第 5 行开始。对于列,它们只能从 0 到 3 开始。我们需要在我们的两个 for 循环中考虑这些约束:

// Upper Left to Bottom Right Diagonal Check
for (int row = 3; row <= 5 && !winFound; row++)
{
    rowFound = row;
    for (int col = 0; col <= 3 && !winFound; col++)
    {
        colFound = col;
        if (board[row, col] != null)
        {
            // are the next 3 diagonal the same?
            // Upper Left to Bottom Right direction!
            winFound =
                (board[row, col] == board[row - 1, col + 1]) &&
                (board[row, col] == board[row - 2, col + 2]) &&
                (board[row, col] == board[row - 3, col + 3]);                        
        }
    }        
}

这是进行右上角到左下角对角线检查的一种方法:

// Diagonal Check - Upper Right to Bottom Left:
for (int row = 3; row <= 5 && !winFound; row++)
{
    rowFound = row;
    for (int col = 3; col <= 6 && !winFound; col++)
    {
        colFound = col;
        if (board[row, col] != null)
        {
            // are the next 3 diagonal the same?
            // Upper Right to Bottom Left direction!
            winFound =
                (board[row, col] == board[row - 1, col - 1]) &&
                (board[row, col] == board[row - 2, col - 2]) &&
                (board[row, col] == board[row - 3, col - 3]);
        }
    }
}