How do I know if a function was called by another form?

0

How do I tell if a function has been called by another form?

For example:

On my form 1, I have a method that calls another form:

private void btnFormulario2_Click(object sender, EventArgs e)
{
   frmFormulario2 frm2 = new frmFormulario2();
   frm2.Show();    
}  

On my form 2, I have this method that writes to the database:

private void btnSalvar_Click(object sender, EventArgs e)
{
   GravaNoBanco();    
}  

Is there a way for Form 1 to know if Form 2 called the function that writes to the bank?

    
asked by anonymous 17.11.2014 / 14:45

1 answer

1

Yes, there is this possibility, you can do the following, instead of form.Show() you can use a form.ShowDialog()

bool clicado = false;
private void btnFormulario2_Click(object sender, EventArgs e)
{
   frmFormulario2 frm2 = new frmFormulario2();
   DialogResult result = frm2.ShowDialog();    
   if(result == DialogResult.OK)
   {
       clicado = true;
   }
} 

and on your form2

private void btnSalvar_Click(object sender, EventArgs e)
{
   GravaNoBanco(); 
   DialogResult = DialogResult.OK;   
}  

If you do not want to do with ShowDialog , you will have to create a static global variable on your form1

public static bool clicado = false;

and on your form2 when you click save

private void btnSalvar_Click(object sender, EventArgs e)
{
   GravaNoBanco();
   Form1.clicado = true;
}     

There are better ways, but that already works.

    
17.11.2014 / 14:56