How can I execute a function that is in another form?

Asked

Viewed 709 times

2

I’ve got two presses, one of them I’ve got:

namespace J13_Rouparia_CS
{
    public partial class frmMain : Form
    {
          public static int LocPesq; 
          public static string peqNume;
          public static string peqArti;

          public void UpdatePesquisa()
          {
              switch (LocPesq)
              {
                 case 1:
                     txtSRnum.Text = peqNume;
                     txtSRartigo.Text = peqArti;
                     break;
                 case 2:
                     txtELnum.Text = peqNume;
                     txtELartigo.Text = peqArti;
                     break;
                 case 3:
                     txtSLnum.Text = peqNume;
                     txtSLartigo.Text = peqArti;
                     break;
              }  
         }
    }
}

The next I’d like to run the UpdatePesquisa(), using frmMain.UpdatePesquisa(), but the method UpdatePesquisa does not appear in the list. How can I resolve this situation?

2 answers

1

You need to have your own variable form instantiated in the location you are calling the method UpdateProgress().

Your form is already active? Otherwise, you will have to instantiate the variable and call the method:

frmMain fMain = new frmMain();
fMain.UpdatePesquisa();

If your form is already active, you have two options:

  • Send the main form as a meter in the constructor of the class you will use:

    public partial class frmMain : Form {
       public frmMain(FormReferencia x) {…}
    }
    
  • Use the function Application.OpenForms["<nome da classe>"]:

    Form1 f1 = (Form1)Application.OpenForms["Form1"];
    
  • I crossed out the first sentence of your answer because it’s incorrect, actually the method can only be viewed outside the class if it’s public, but I didn’t want to change your answer and let you edit it, okay? I also forgot to change the name of the method of UpdateProgress() for UpdatePesquisa(), I made a second edition, but I don’t know if it’ll be accepted, so anything you can change, please. Now, about this first suggestion you gave with the active form, I think it is difficult that the second form is open before of frmMain, which, by name, is the main!

  • Peter, Thanks for the help! Really got confused the answer. About the name of the form, I also think it is already active, but I thought better to inform the solution if it is not.

  • This excerpt from your reply: "You will not be able to visualize the method because it is public" is wrong. The method of being public is not the reason why it cannot be visualized, on the contrary, if it were not public, then it would not be visualized. So I had scratched that part out, I hoped you’d delete it! And there’s another UpdateProgress() wrong in the first paragraph.

0

Assuming you want to access the form frmMain from another evoked by that same Form, can do as follows:

frmMain

/// <summary>
/// Método localizado em "frmMain"
/// Criar uma nova instância do novo form
/// passando este como "Owner"
/// </summary>
void AbrirNovoForm()
{
    frmNovoForm frmNovoForm = new frmNovoForm();
    frmNovoForm.Show(this);
}

frmNovoForm

/// <summary>
/// Método localizado em "frmNovoForm"
/// Fazer cast do "Owner" para "frmMain" e evocar o método "UpdatePesquisa()"
/// </summary>
void UpdatePesquisa() => (Owner as frmMain).UpdatePesquisa();

Browser other questions tagged

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