再会。我有一个 Form1 和两个类的程序。当我想在其中一个类中使用 label1 时,它会显示错误名称 label1 在当前上下文中不存在。我如何在 Class 中使用 label1 来显示文本?
namespace snake_game
{
public class had
{
private Rectangle[] snakeRec;
private SolidBrush brush;
private int x, y, width, height;
public Rectangle[] SnakeRec
{
get { return snakeRec; }
}
public had()
{
snakeRec = new Rectangle[3];
brush = new SolidBrush(Color.Black);
x = 20;
y = 0;
width = 10;
height = 10;
for (int i = 0; i < snakeRec.Length; i++)
{
snakeRec[i] = new Rectangle(x, y, width, height);
x -= 10;
}
}
...................................................
..................................................... ......................
public void drawSnake()
{
for (int i = snakeRec.Length - 1; i > 0; i--)
{
snakeRec[i] = snakeRec[i - 1];
for (int j = 1; j < snakeRec.Length; j++)
if (snakeRec[i].X == snakeRec[j].X && snakeRec[i].Y == snakeRec[j].Y)
{
label1.Text = "------"; //here is problem//
}
}
}