Form opening another form inside a splitcontainer

1

I have a main form divided by a splitcontaniner . When I click on a panel1 button, a form is loaded into panel2 .

    private void btnCadastrarConta_Click(object sender, EventArgs e)
    {
        stcMenuPrincipal.Panel2.Controls.Clear();
        frmCadastroConta formulario = new frmCadastroConta();
        formulario.TopLevel = false;
        formulario.AutoScroll = true;
        stcMenuPrincipal.Panel2.Controls.Add(formulario);
        formulario.Show();
    }

This form (frmCadastroConta) that was loaded in panel2 has a button that displays another form ( frmInformacoesAdicionais ).

How can I frmInformacoesAdicionais be loaded in panel2 instead of frmCadastroConta ?

Use C # , Net 3.5 .

    
asked by anonymous 04.02.2014 / 16:55

3 answers

1

You can remove frmCadastroConta and add frmAdditional as you did to add > frmCadastroConta .

    
10.02.2014 / 19:53
0
  

Note: I know the question is old, but I think the answer might help other users.

Introduction

One solution to this issue would be Events , the events allow classes or objects to notify other classes or objects that a certain action has been performed, enabling those who have signed those events to perform some task related to it.

We often use events, a basic example would be the Click event of the Button component, which allows us to do something when a button is clicked.

Codes

I set up a scenario equal to what you reported, set the name of Form as frmPrincipal :

using System;
using System.Windows.Forms;

namespace App.Teste
{
    public partial class frmPrincipal : Form
    {
        public frmPrincipal()
        {
            InitializeComponent();
        }

        private void btnCadastrarConta_Click(object sender, EventArgs e)
        {
            stcMenuPrincipal.Panel2.Controls.Clear();
            frmCadastroConta formulario = new frmCadastroConta();
            formulario.TopLevel = false;
            formulario.AutoScroll = true;
            formulario.NextForm += new frmCadastroConta.NextFormEventHandler(formulario_NextForm); // assina o evento
            stcMenuPrincipal.Panel2.Controls.Add(formulario);
            formulario.Show();
        }

        // método que irá tratar o evento
        private void formulario_NextForm(object source, string name)
        {
            ((Form)source).Close();
            if (name == "frmInformacoesAdicionais")
            {
                frmInformacoesAdicionais formulario = new frmInformacoesAdicionais();
                formulario.TopLevel = false;
                formulario.AutoScroll = true;
                stcMenuPrincipal.Panel2.Controls.Add(formulario);
                formulario.Show();
            }
        }
    }
}

In frmCadastroConta , I created the btnInformacoesAdicionais button that will trigger the event to load frmInformacoesAdicionais :

using System.Windows.Forms;

namespace App.Teste
{
    public partial class frmCadastroConta : Form
    {

        public delegate void NextFormEventHandler(object source, string name);
        public event NextFormEventHandler NextForm;

        public frmCadastroConta()
        {
            InitializeComponent();
        }

        private void btnInformacoesAdicionais_Click(object sender, System.EventArgs e)
        {
            OnNextForm("frmInformacoesAdicionais");
        }

        public virtual void OnNextForm(string name)
        {
            // verifica se o evento possui algum assinante
            if (NextForm != null)
                NextForm(this, name);
        }
    }
}

The frmInformacoesAdicionais is nothing special, I only created a Form with a Label to identify it.

Explanations

The Form that will generate the event is frmCadastroConta , and what will receive (sign) the event is frmPrincipal , that is, frmCadastroConta will notify frmPrincipal through the event NextForm that a new Form must be loaded.

These lines below make it possible to use events:

public delegate void NextFormEventHandler(object source, string name);
public event NextFormEventHandler NextForm;

...

public virtual void OnNextForm(string name)
{
    // verifica se o evento possui algum assinante
    if (NextForm != null)
        NextForm(this, name);
}

Declared a Delegate , the event itself and a method to call the event.

  

Note: In the delegate statement you can enter the parameters you think necessary, I have added source to identify which object generated the event and name to identify which Form % to charge.   

Note 2: The name of the event delegate, event, and method (consider using the On prefix as a good practice for this), you choose.

Once you have done this you should sign the event in frmPrincipal as shown in the line:

formulario.NextForm += new frmCadastroConta.NextFormEventHandler(formulario_NextForm);
  

Note: You can set the name you think best for your method, not forgetting to declare it with the same parameters as delegate .

The formulario_NextForm method will treat the generated event, which in this case will close the Form that sent the event and load the Form passed by the name parameter.

Conclusion

Using Events in this case, leaves your code more clean and organized, getting rid of "gambiarras".

References

.NET Forms Communication
C# - Events
Handling and canceling event signing (C # Programming Guide)

    
04.12.2014 / 03:05
0

You can create an object related to the other form before entering this method of clicking the button, hence to avoid memory leak, and erase that form2 that is inside of form1 and creates form3 instead of form2 in the button method of this form2.

ce would put it there, for example:

TForm3 form3 = new TForm3();
form3.TopLevel = false;
form1.panel2.Controls.Add(form3);
form3.Show();
Close();

Thus, form3 would be created, but form2 would be removed from memory. If this is not what you need, if you need to 'hide' form2, just create the form3 and call it inside the form2 button, doing a Hide on that form2.

TForm3 form3 = new TForm3();
form3.TopLevel = false;
form1.panel2.Controls.Add(form3);
form3.Show();
Hide();

Although this way you can make your program take up more memory, require more processing, for test cases it even works.

    
03.12.2014 / 20:21