簡體   English   中英

啟動應用程序時出現 System.StackOverflowException

[英]System.StackOverflowException when launching application

當我啟動我的應用程序時,它給了我一個錯誤“Form1 Test = new Form1();” 在我的 class 中。 這是我的代碼。 我想使用表單中的標簽,因此我使用了“form1 test”。

我做了一個 class ,所以我可以在我的 Mainform 中調用我的方法,因為我需要用類對我的應用程序進行編碼。 當我第一次啟動該應用程序時,它可以工作,但再次嘗試后它就不再工作了。

主要形式:

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();
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM