System.StackOverflowException 启动应用程序时
System.StackOverflowException when launching application
当我启动我的应用程序时,出现错误“Form1 Test = new Form1();”在我的 class 中。这是我的代码。我想使用我表单中的标签,因此我使用了“form1 test”。
我创建了一个 class,这样我就可以在我的 Mainform 中调用我的方法,因为我需要使用 classes 对我的应用程序进行编码。当我第一次启动该应用程序时它运行良好,但在再次尝试后它不再运行了。
主要形式:
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 Tester
{
public partial class Form1 : Form
{
Zombie zombie = new Zombie();
int levens = 3;
public Form1()
{
InitializeComponent();
test1.Text = "Levens: " + "" + levens;
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void Zombie()
{
foreach (Control control in Controls)
{
PictureBox pic = control as PictureBox;
if (pic != null)
{
pic.Top += 1;
if (pic.Top > 600 && pic.Visible == true)
{
pic.Top = 0;
test1.Text = $"Levens: {--levens}";
}
else if (pic.Top > 600 && pic.Visible == false)
{
pic.Visible = true;
pic.Top = 0;
}
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
zombie.MakeZombie(5, this);
}
}
}
Class:
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 Tester
{
class Zombie
{
Random random = new Random();
Form1 Test = new Form1();
private int score = 0;
public void MakeZombie(int aantal, Form formInstance)
{
for (int i = 0; i < aantal; i++)
{
PictureBox picture = new PictureBox();
picture.Image = Properties.Resources.ZombieDik;
picture.Size = new Size(200, 200);
picture.Location = new Point(random.Next(1500), 0);
picture.SizeMode = PictureBoxSizeMode.Zoom;
picture.Click += zombie_Click;
picture.BackColor = Color.Transparent;
formInstance.Controls.Add(picture);
}
}
void zombie_Click(object sender, EventArgs e)
{
PictureBox pic = sender as PictureBox;
pic.Visible = false;
score++;
Test.label2.Text = $"Score: {score}";
Test.Controls.Remove(pic);
pic.Dispose();
}
}
}
在 zombie_Click()
中,您可以从发件人本身获取对表单的引用:
void zombie_Click(object sender, EventArgs e)
{
PictureBox pic = sender as PictureBox;
Form1 f1 = pic.FindForm() as Form1;
score++;
f1.label2.Text = $"Score: {score}";
pic.Dispose();
}
当我启动我的应用程序时,出现错误“Form1 Test = new Form1();”在我的 class 中。这是我的代码。我想使用我表单中的标签,因此我使用了“form1 test”。
我创建了一个 class,这样我就可以在我的 Mainform 中调用我的方法,因为我需要使用 classes 对我的应用程序进行编码。当我第一次启动该应用程序时它运行良好,但在再次尝试后它不再运行了。
主要形式:
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 Tester
{
public partial class Form1 : Form
{
Zombie zombie = new Zombie();
int levens = 3;
public Form1()
{
InitializeComponent();
test1.Text = "Levens: " + "" + levens;
}
private void Form1_Load(object sender, EventArgs e)
{
}
public void Zombie()
{
foreach (Control control in Controls)
{
PictureBox pic = control as PictureBox;
if (pic != null)
{
pic.Top += 1;
if (pic.Top > 600 && pic.Visible == true)
{
pic.Top = 0;
test1.Text = $"Levens: {--levens}";
}
else if (pic.Top > 600 && pic.Visible == false)
{
pic.Visible = true;
pic.Top = 0;
}
}
}
}
private void timer1_Tick(object sender, EventArgs e)
{
zombie.MakeZombie(5, this);
}
}
}
Class:
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 Tester
{
class Zombie
{
Random random = new Random();
Form1 Test = new Form1();
private int score = 0;
public void MakeZombie(int aantal, Form formInstance)
{
for (int i = 0; i < aantal; i++)
{
PictureBox picture = new PictureBox();
picture.Image = Properties.Resources.ZombieDik;
picture.Size = new Size(200, 200);
picture.Location = new Point(random.Next(1500), 0);
picture.SizeMode = PictureBoxSizeMode.Zoom;
picture.Click += zombie_Click;
picture.BackColor = Color.Transparent;
formInstance.Controls.Add(picture);
}
}
void zombie_Click(object sender, EventArgs e)
{
PictureBox pic = sender as PictureBox;
pic.Visible = false;
score++;
Test.label2.Text = $"Score: {score}";
Test.Controls.Remove(pic);
pic.Dispose();
}
}
}
在 zombie_Click()
中,您可以从发件人本身获取对表单的引用:
void zombie_Click(object sender, EventArgs e)
{
PictureBox pic = sender as PictureBox;
Form1 f1 = pic.FindForm() as Form1;
score++;
f1.label2.Text = $"Score: {score}";
pic.Dispose();
}