c# - 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++ - 是否可以返回对 std::vector 中数据的引用,并在向量超出范围后保留该引用?
- exit - Pine 脚本 - 开盘价时退出 > 入场价“又名第一个盈利开盘价”
- python - 使用 lambda 函数和 zip 过滤
- python - Python 3:使用 FuncFormatter 设置辅助 y 轴标签的问题
- angular - ag-grid 编辑数据,然后保存更新的数据并发送到数据库
- azure-ad-b2c - AAD B2C - 用户是否有权更改自己的自定义属性?
- javascript - 使用单选按钮检索值的购物车问题
- wpf - 如何通过 Behavior 和 VisualStateManager 将 TextBox 设置为特定的内置 VisualState
- kubernetes - 错误:未提供配置,尝试设置 KUBERNETES_MASTER 环境变量
- mysql - Error_code: 2005 - Mysql Operator - 连接到主服务器时出错