Home > Mobile >  System.StackOverflowException when launching application
System.StackOverflowException when launching application

Time:12-09

When I launch my application it gives me an error at " Form1 Test = new Form1();" in my class. Here is my code. I want to use labels from my form so therefore I used "form1 test".

I made a class so I can call my methods from it in my Mainform as I need to code my application with classes. When I launched the application for the first time it worked, but then after trying again it didn't work anymore.

Main form:

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


CodePudding user response:

In zombie_Click() you can get a reference to the Form from the sender itself:

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