表单 window 未显示(C# .NET 5.0,Visual Studio 2019)
Form window not showing up (C# .NET 5.0, Visual Studio 2019)
我尝试复制扫雷作为使用 Windows 表单编码的小练习。
一段时间后,当我运行程序时,窗体既不显示也不出现在任务栏中。
最终,我能够将问题追查到我编写的函数中,该函数在 Form1 构造函数中调用。每当它在其构造函数中时,每当我按 F5 时,表单都不会出现。没有显示任何错误消息,只是 window 没有弹出。
这是我写的代码。导致问题的函数是 Form1 构造函数中调用的 Setup() 函数:
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 Minesweeper
{
public partial class Form1 : Form
{
private int bombs = 40;
private Size gridSize = new Size (20, 20);
private Random random = new Random();
private int[,] values;
public Form1()
{
InitializeComponent();
Setup();
}
//Setup / Game start
private void Setup()
{
//Set grid size
values = new int[gridSize.Width, gridSize.Height];
//Place bombs
if (bombs < gridSize.Width * gridSize.Height)
{
for (int i = 0; i < bombs; i++)
{
Point p = new Point(random.Next(0, gridSize.Width), random.Next(0, gridSize.Height));
while (values[p.X, p.Y] != 9)
{
p = new Point(random.Next(0, gridSize.Width), random.Next(0, gridSize.Height));
}
values[p.X, p.Y] = 9;
}
}
//Calculate values
for (int x = 0; x < gridSize.Width; x++)
{
for (int y = 0; y < gridSize.Height; y++)
{
if (values[x, y] >= 9)
{
ChangeValueAt(x - 1, y - 1, 1);
ChangeValueAt(x - 1, y, 1);
ChangeValueAt(x - 1, y + 1, 1);
ChangeValueAt(x, y - 1, 1);
ChangeValueAt(x, y + 1, 1);
ChangeValueAt(x + 1, y - 1, 1);
ChangeValueAt(x + 1, y, 1);
ChangeValueAt(x + 1, y + 1, 1);
}
}
}
}
//Change value in grid
private void ChangeValueAt(int x, int y, int changeBy)
{
if (values.GetLength(0) > x && values.GetLength(1) > y && values[x, y] != 9)
{
values[x, y] += changeBy;
}
}
}
}
有人知道为什么会这样吗?
PS: 我对编码很陌生,我知道我的代码通常不是很整洁,可能会有一些不好的内存使用,但我想知道是否有人可以帮助我这里。
那是因为 while 循环永远不会结束。 for 循环第一次运行时,还没有 value[,x,y]
被设置为 9
。
因此,while 条件 values[p.X, p.Y] != 9
将始终为真。
我会写
for (int i = 0; i < bombs; i++) {
Point p;
do {
p = new Point(random.Next(0, gridSize.Width),
random.Next(0, gridSize.Height));
} while (values[p.X, p.Y] == 9);
values[p.X, p.Y] = 9;
}
此外,还必须测试下限:
private void ChangeValueAt(int x, int y, int changeBy)
{
if (0 <= x && x < values.GetLength(0) &&
0 <= y && y < values.GetLength(1) &&
values[x, y] != 9) {
values[x, y] += changeBy;
}
}
我尝试复制扫雷作为使用 Windows 表单编码的小练习。
一段时间后,当我运行程序时,窗体既不显示也不出现在任务栏中。
最终,我能够将问题追查到我编写的函数中,该函数在 Form1 构造函数中调用。每当它在其构造函数中时,每当我按 F5 时,表单都不会出现。没有显示任何错误消息,只是 window 没有弹出。
这是我写的代码。导致问题的函数是 Form1 构造函数中调用的 Setup() 函数:
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 Minesweeper
{
public partial class Form1 : Form
{
private int bombs = 40;
private Size gridSize = new Size (20, 20);
private Random random = new Random();
private int[,] values;
public Form1()
{
InitializeComponent();
Setup();
}
//Setup / Game start
private void Setup()
{
//Set grid size
values = new int[gridSize.Width, gridSize.Height];
//Place bombs
if (bombs < gridSize.Width * gridSize.Height)
{
for (int i = 0; i < bombs; i++)
{
Point p = new Point(random.Next(0, gridSize.Width), random.Next(0, gridSize.Height));
while (values[p.X, p.Y] != 9)
{
p = new Point(random.Next(0, gridSize.Width), random.Next(0, gridSize.Height));
}
values[p.X, p.Y] = 9;
}
}
//Calculate values
for (int x = 0; x < gridSize.Width; x++)
{
for (int y = 0; y < gridSize.Height; y++)
{
if (values[x, y] >= 9)
{
ChangeValueAt(x - 1, y - 1, 1);
ChangeValueAt(x - 1, y, 1);
ChangeValueAt(x - 1, y + 1, 1);
ChangeValueAt(x, y - 1, 1);
ChangeValueAt(x, y + 1, 1);
ChangeValueAt(x + 1, y - 1, 1);
ChangeValueAt(x + 1, y, 1);
ChangeValueAt(x + 1, y + 1, 1);
}
}
}
}
//Change value in grid
private void ChangeValueAt(int x, int y, int changeBy)
{
if (values.GetLength(0) > x && values.GetLength(1) > y && values[x, y] != 9)
{
values[x, y] += changeBy;
}
}
}
}
有人知道为什么会这样吗?
PS: 我对编码很陌生,我知道我的代码通常不是很整洁,可能会有一些不好的内存使用,但我想知道是否有人可以帮助我这里。
那是因为 while 循环永远不会结束。 for 循环第一次运行时,还没有 value[,x,y]
被设置为 9
。
因此,while 条件 values[p.X, p.Y] != 9
将始终为真。
我会写
for (int i = 0; i < bombs; i++) {
Point p;
do {
p = new Point(random.Next(0, gridSize.Width),
random.Next(0, gridSize.Height));
} while (values[p.X, p.Y] == 9);
values[p.X, p.Y] = 9;
}
此外,还必须测试下限:
private void ChangeValueAt(int x, int y, int changeBy)
{
if (0 <= x && x < values.GetLength(0) &&
0 <= y && y < values.GetLength(1) &&
values[x, y] != 9) {
values[x, y] += changeBy;
}
}