首页 > 解决方案 > C# WinForm 二维数组

问题描述

我是 C# 的初学者。我需要专业的帮助。首先,我有像 C# WindowsForm 代码这样的动态数组代码。这个程序是用代码编写的,没有使用 Form。

程序的运行原理是在当前代码状态下使用上下左右方向键从(0,0)点到(9,9)点。

问题 1. 每当我使用箭头键上下左右移动坐标时,我想在按钮上制作一个红色边框,但我不知道怎么做。

问题2。例如,在点(0,0),我想让它只移动到右边和底部。同样,在 (2,0) 点,它应该设置为不能只向上移动。

问题 3. 我想实现 20 颗炸弹随机撒在中间,如果炸弹被踩到,则会显示一条适当的消息并初始化为 (0,0) 点。

谢谢你。

在此处输入图像描述

    namespace StartAndEnd
{
    public partial class Form1 : Form
    {
        private Button[,] arryButton;

        private bool[,] locationMe;

        private Point[,] arryLocation;
        private Size[,] arrySize;

        private Size FormSize;

        private int iWidthGab = 1;
        private int iHeightGab = 1;

        private int iCtrlwidth = 50;
        private int iCtrlHeight = 50;

        private int iX = 10;
        private int iY = 10;

        public Form1()
        {
            InitializeComponent();

            SetInit();
            SetEvent();
        }
        private void SetInit()
        {
            this.KeyPreview = true;

            arryButton = new Button[iX, iY];
            arryLocation = new Point[iX, iY];
            arrySize = new Size[iX, iY];
            locationMe = new bool[iX, iY];

            for (int y = 0; y < iY; y++)
            {
                for (int x = 0; x < iX; x++)
                {
                    Button btn = new Button();

                    btn.Text = (x.ToString() + "," + y.ToString());
                    btn.Name = (x.ToString() + "," + y.ToString());

                    btn.BackColor = Color.Gray;

                    btn.FlatStyle = FlatStyle.Flat;
                    btn.Width = iCtrlwidth;
                    btn.Height = iCtrlHeight;

                    if (x > 0)
                    {
                        Button pbtn = arryButton[x - 1, y];
                        btn.Left = pbtn.Left + pbtn.Width + iWidthGab;

                    }
                    else
                    {
                        btn.Left = iWidthGab;
                    }
                    if (y > 0)
                    {
                        Button pbtn = arryButton[x, y - 1];
                        btn.Top = pbtn.Top + pbtn.Height + iHeightGab;
                    }
                    else
                    {
                        btn.Top = iHeightGab;
                    }

                    btn.MouseDown += Btn_MouseDown;
                    btn.MouseUp += Btn_MouseUp;

                    this.Controls.Add(btn);

                    arrySize[x, y] = btn.Size;
                    arryLocation[x, y] = btn.Location;
                    arryButton[x, y] = btn;

                    locationMe[x, y] = false;
                }
            }

            Button LastButton = arryButton[iX - 1, iY - 1];
            FormSize = new Size(LastButton.Left + LastButton.Width + iWidthGab, LastButton.Top + LastButton.Height + iHeightGab); // 만들어진 객체들을 포함하는 전쳋 창의 최소 크기 지정

            locationMe[0, 0] = true;
            this.ClientSize = FormSize;
        }

        private void Btn_MouseUp(object sender, MouseEventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackColor = Color.Gray;
        }

        private void Btn_MouseDown(object sender, MouseEventArgs e)
        {
            Button btn = (Button)sender;
            btn.BackColor = Color.Green;
        }

        private void SetEvent()
        {
            this.Resize += Form1_Resize;
        }
        private void Form1_Resize(object sender, EventArgs e)
        {
            this.DoubleBuffered = false;

            if (this.ClientSize.Width < FormSize.Width)
                this.ClientSize = new Size(FormSize.Width, this.ClientSize.Height);

            if (this.ClientSize.Height < FormSize.Height)
                this.ClientSize = new Size(this.ClientSize.Width, FormSize.Height);

            int iRWidth = this.ClientSize.Width - FormSize.Width;
            int iRHeight = this.ClientSize.Height - FormSize.Height;

            for (int y = 0; y < iY; y++)
            {
                for (int x = 0; x < iX; x++)
                {
                    int iXGap = iRWidth / iX;
                    int iYGap = iRHeight / iY;

                    int iCtrlwidthMod = iCtrlwidth + iXGap;
                    int iCtrlheightMod = iCtrlHeight + iYGap;

                    this.Text = iRWidth + "," + iRHeight + "|" + iCtrlwidthMod + "," + iCtrlheightMod; ;

                    Button btn = arryButton[x, y];

                    if (x > 0)
                    {
                        Button pbtn = arryButton[x - 1, y];
                        btn.Left = pbtn.Left + iCtrlwidthMod + iWidthGab;
                        btn.Width = iCtrlwidthMod;
                    }
                    else
                    {
                        btn.Left = iWidthGab;
                        btn.Width = iCtrlwidthMod;
                    }
                    if (y > 0)
                    {
                        Button pbtn = arryButton[x, y - 1];
                        btn.Top = pbtn.Top + iCtrlheightMod + iHeightGab;
                        btn.Height = iCtrlheightMod;
                    }
                    else
                    {
                        btn.Top = iHeightGab;
                        btn.Height = iCtrlheightMod;
                    }

                }

            }
            this.DoubleBuffered = true;
        }

    }

}

标签: c#arrayswinforms

解决方案


推荐阅读