csanchez
csanchez

Reputation: 91

Exception of type 'System.StackOverflowException' was thrown in c#

I'm trying to call a form

public partial class MenuForm : Form
    {

        Ventanas v = new Ventanas();
        EnfermoRep reporteEnfermo = new EnfermoRep();
        public MenuForm()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void rptEnfermo_Click(object sender, EventArgs e)
        {
            v.CargarVentana(reporteEnfermo, this.panel1);
        }
    } 

but when I run it,marks "Exception of type 'System.StackOverflowException' was thrown" when i'm instancing the "EnfermoRep".

This is the other class

public partial class EnfermoRep : Form
    {

        Ventanas v = new Ventanas();
        MenuForm menuForm = new MenuForm();
        public EnfermoRep()
        {
            InitializeComponent();
        }

        private void EnfermoRep_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'bd.Enfermo' table. You can move, or remove it, as needed.
            this.EnfermoTableAdapter.Fill(this.bd.Enfermo);

            this.reportViewer1.RefreshReport();
        }

        private void btnVolver1_Click(object sender, EventArgs e)
        {
            v.CargarVentanas(menuForm, this.enfermoRep);
        }
    }

And also i'm using this so I can alternate between panels in just one form:

class Ventanas
    {
        public void CargarVentana(object sonform, Panel panel)
        {
            panel.Controls.Clear();
            Form fh = sonform as Form;
            fh.TopLevel = false;
            fh.FormBorderStyle = FormBorderStyle.None;
            fh.Dock = DockStyle.Fill;
            panel.Controls.Add(fh);
            panel.Tag = fh;
            fh.Show();
        }
}

In every form I make, I put a panel dock in container, so I can call it with The "Ventana" class

Any idea how to solve that error. Thanks! :D

Upvotes: 1

Views: 12062

Answers (2)

Sheradil
Sheradil

Reputation: 467

Hope I'm not wrong, but a simple fix (possibly not a good one) would be:

public partial class MenuForm : Form
{

    Ventanas v = new Ventanas();
    EnfermoRep reporteEnfermo;
    public MenuForm()
    {
        InitializeComponent();
        reporteEnfermo = new EnfermoRep(this);
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void rptEnfermo_Click(object sender, EventArgs e)
    {
        v.CargarVentana(reporteEnfermo, this.panel1);
    }
} 

public partial class EnfermoRep : Form
{

    Ventanas v = new Ventanas();
    MenuForm menuForm;
    public EnfermoRep(MenuForm MF)
    {
        menuForm = MF;
        InitializeComponent();
    }

    private void EnfermoRep_Load(object sender, EventArgs e)
    {
        this.EnfermoTableAdapter.Fill(this.bd.Enfermo);

        this.reportViewer1.RefreshReport();
    }

    private void btnVolver1_Click(object sender, EventArgs e)
    {
        v.CargarVentanas(menuForm, this.enfermoRep);
    }
}

Not sure though if that will resolve your problem overall, but it does get rid of the stackoverflow. It is just a small change. You just pass your MenuForm as a parameter instead of creating a new one inside of the EnfermoRep class. In this solution it is neccessary to create the MenuForm first. It is not difficult to make changes neccessary to be able to create the EnfermoRep first. I will leave that as a task ;)

Upvotes: 1

Flydog57
Flydog57

Reputation: 7111

When you construct MenuForm, this code runs:

EnfermoRep reporteEnfermo = new EnfermoRep();` 

When you construct EnfermoRep, this code runs:

MenuForm menuForm = new MenuForm();

You end up recursing forever. If you look at your Stack panel, you will see the stack creating a EnfermoRep and then a MenuForm and repeating that sequence forever. You'll need to detangle your code. Generally, if you want to run both forms at the same time, create the two forms somewhere else (in your Main routine, for example) and then run them from there.

Upvotes: 1

Related Questions