How to know if a function has been called by another form?

Asked

Viewed 216 times

0

How to know if a function has been called by another form?

For example:

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

In my Form 2, I have this method that records in the bank:

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

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

  • Yes, you need to create an event for this and notify interested parties. In case the form one would have to sign the event to be notified. http://msdn.microsoft.com/en-us/library/awbftdfh.aspx e

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 in its form2

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

If you don’t want to do ShowDialog, you will need to create a static global variable in your Form1

public static bool clicado = false;

and in your form2 by clicking save

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

There are better ways, but it already solves.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.