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)