I wrote all the code, I launch the program, but the Windows form is empty. Help me please!
The Windows form starts, but it is empty, although there should be cells. Nothing is displayed on the Windows form.
我写了所有的代码,我启动了程序,但Windows窗体是空的。请帮帮我!Windows窗体启动,但它是空的,尽管应该有单元格。Windows窗体上不显示任何内容。
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 WinFormsApp1
{
public partial class Form1 : Form
{
public const int mapSize = 8;
public const int cellSize = 50;
public int[,] map = new int[mapSize, mapSize];
public Button[,] buttons = new Button[mapSize, mapSize];
public Form1()
{
InitializeComponent();
}
private void Init()
{
ConfigureMapSize();
InitMap();
InitButtons();
}
private void ConfigureMapSize()
{
this.Width = mapSize * cellSize + 20;
this.Height = (mapSize +1) * cellSize;
}
private void InitMap()
{
for(int i = 0; i < mapSize; i++)
{
for (int j = 0; j < mapSize; j++)
{
map[i, j] = 0;
}
}
}
private void InitButtons()
{
for (int i = 0; i < mapSize; i++)
{
for (int j = 0; j < mapSize; j++)
{
Button button = new Button();
button.Location = new Point(j * cellSize, i * cellSize);
button.Size = new Size(cellSize, cellSize);
this.Controls.Add(button);
buttons[i, j] = button;
}
}
}
}
}
更多回答
Seems like you need to call Init() in the form's Load or Shown event.
您似乎需要在表单的Load或Showed事件中调用Init()。
Do you call Init()
?
您调用Init()了吗?
I removed: "I don't know what to write anymore. I don't know what to write anymore. I don't know what to write anymore" from your question, hope that can agree with that ? 😁
我从你的问题中删除了:“我不知道该写什么了。我不知道该写什么了。我不知道该写什么了”,希望你能同意这一点?😁
please add details about how you instantiate and show the form - without that no one can help
请添加有关如何实例化和显示表单的详细信息-如果没有这些,没有人能提供帮助
优秀答案推荐
Change your constructor and it will work:
更改您的构造函数,它将工作:
public Form1()
{
InitializeComponent();
Init(); // Add this line
}
更多回答
我是一名优秀的程序员,十分优秀!