Access methods from another Form

Asked

Viewed 767 times

4

I have a form called TelaInicio where I have a method CarregarGrid() I would like to access this method through my other form Cadastrar I’ve left the method CarregarGrid()as public but still do not have access to it I instated the form TelaInicio thus

 public Cadastrar(TelaInicio telainicio)
    {
        InitializeComponent();
        this.telainicio = telainicio;
    }

and in the TelaInicio I call him on a button

        private void Cadastrar_Click(object sender, EventArgs e)
    {
        Cadastrar form = new Cadastrar(this);
        form.ShowDialog();
    }

and in the TelaInicio my method is like public void CarregaGrid(), but still in the other form when I try to access it says that the method does not exist

public void CarregarGrid()
    {
        try{
        //indico o número de colunas
        dgvDados.ColumnCount = 14;
        objConnection = new MySqlConnection(caminho);
        //instância do comando onde passo
        //o sql e a conexão como parâmetro
        objComando =  new MySqlCommand("SELECT * FROM checagens", objConnection);
        //abro a conexão
        objConnection.Open();
        //instâncio o leitor
        var leitor = objComando.ExecuteReader();
        //enquanto leitor está lendo
        while (leitor.Read())
        {
            //insiro os dados no dgvDados
            dgvDados.Rows.Add(leitor[0].ToString(),
                leitor[1].ToString(),
                leitor[2].ToString(),
                leitor[3].ToString(),
                leitor[4].ToString(),
                leitor[5].ToString(),
                leitor[6].ToString(),
                leitor[7].ToString(),
                leitor[8].ToString(),
                leitor[9].ToString(),
                leitor[10].ToString(),
                leitor[11].ToString(),
                leitor[12].ToString(),
                leitor[13].ToString());

        }
    }
        catch(Exception ex){
            MetroFramework.MetroMessageBox.Show(this, "" + ex, "Erro", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }
        finally{
        //fecho conexão
        objConnection.Close();
        }
    }

I’m calling the method inside the button Register in the form Cadastrar

  if (cadchecagem(checagem))
        {

            MetroFramework.MetroMessageBox.Show(this, "Dados cadastrados com sucesso ", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            Utilidades.Funcoes.LimparCampos(gbchecagem);
            CarregarGrid();

        }
    }

It presents the error:

The name 'Carregargrid' does not exist in the Current context

  • Where is the "Carregargrid" method? Preferably put the 2 classes in your question to facilitate analysis

  • I edited the question and added the method but I believe it will not make a difference you see it

  • I did not locate where you are trying to access the method of one form through another, you need to include the code snippet where you are presenting the error.

  • You can put it as static or better create a manager to access the entire class.

  • Hello Patrick. Put the code part where you call the method CarregarGrid() from the Cadastrar.

  • @Cypherpotato Edited

Show 1 more comment

1 answer

1


The method does not really exist in the context where it is being called, for it is being called as if it were a member of Cadastrar. It has not been specified where this method is or where you will call it.

You must specify who will call the method. You were right to make it public in the class TelaInicio and instantiate it by parameter in your constructor, now to call this method, consider:

if (cadchecagem(checagem))
    {

        MetroFramework.MetroMessageBox.Show(this, "Dados cadastrados com sucesso ", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        Utilidades.Funcoes.LimparCampos(gbchecagem);

        // Aqui ele estará procurando onde está este método, no seu caso, está no telainicio
        this.telainicio.CarregarGrid();
    }
}

And remember, telainicio must be declared within the Cadastrar as a class member and not a method member:

// declarado e setado no ctor da classe
internal TelaInicio telainicio = null;

public Cadastrar(TelaInicio telainicio)
{
    InitializeComponent();
    this.telainicio = telainicio;
}
  • did not know that the correct way to call the method was this thank you, I also added the internal TelaInicio telainicio = null; but when running the program it gives an error saying Extension method must be defined in a non-generic Static class presents this error in the class TelaInicio

  • I also added the class TelaInicio in the question, there’s nothing different about it I don’t know if I should add something to the class

  • I got it, thanks

Browser other questions tagged

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