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

0

What I want to do is move from one form to another. I call from MenuForm to another call "EmfermoRep", which is fine, but the problem comes when I want to go back from "EnfermoRep" to "MenuForm". I tried to add a button and instantiate "MenuForm", but it sent me that error which is an infinite loop. Some ideas to come back, I would really appreciate it: D!

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

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

It is worth mentioning that I created a class called windows, which allows me to move in Forms through panels (In other words, the program only has one main Form, which calls other but with panels, without ever leaving the main one) No I know if I can understand -.- '

This is the class that allows that:

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();
        }
}
    
asked by csanchez 29.11.2018 в 18:50
source

1 answer

0

You can not use the new to create a new instance of the parent form if you want to share with an existing instance, you must pass that instance, the issue is that you complicate it when programming with panels and forms, because you did not use User Controls User controls are like forms without borders and can be better located within a panel.

public partial class MenuForm : Form
{

    public MenuForm()
    {
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void rptEnfermo_Click(object sender, EventArgs e)
    {
        EnfermoRep reporteEnfermo = new EnfermoRep();
        Ventanas.CargarVentana(reporteEnfermo, this, this.panel1);
    }
} 

public partial class EnfermoRep : Form
{


    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)
    {
        var frm = this.Owner as Form;
        frm.Show();

        this.Close();
    }
}



class Ventanas
{
    public static void CargarVentana(Form sonform, Form owner, Panel panel)
    {
        sonform.TopLevel = false;
        sonform.FormBorderStyle = FormBorderStyle.None;
        sonform.Dock = DockStyle.Fill;
        panel.Controls.Add(fh);

        sonform.Show(owner);
    }
}

apply code changes

Analyze how the owner is defined in order to determine who is the parent of the form you open and thus be able to return But the return closes that form, which would have to be validated if the closure removes it from the Panel, otherwise the panel instance should be passed in order to remove the Panel.Controls

    
answered by 29.11.2018 в 19:53