Pass Controls to a Function

2

I have an interface in Visual C #. In this interface I have a class named T50 that contains all the variables I need.

I've already implemented a button where the object created by the T50 class was saved. I can save and read this object (Using serialize).

But when I reread a saved object I need to fill in the part of the interface that the user already had.

Within my T50 class there is a function to do this.

private void button_t50_carregar_Click(object sender, EventArgs e)
    {
        // Código de ler o dado

        // Usa uma função para preencher os dados
        T50_Data.PreencheInterface(T50_Data);
    }

But within this function I need to access all the Controls of my Form to adjust them (Button, Textbox, DataGridViwe ....).

I know how to get them individually, for example

public void PreencheInterface (T50 T50Data, TextBox text)
{
}

Is there a way for me to pass all of them at the same time?

    
asked by anonymous 01.08.2015 / 21:48

3 answers

3

You could do this:

Pass your Form to the method:

public void Metodo(NomeDoForm form)
{
    form.Controls["btnEsc"].Text = "Escape";
}

And within the method call each component of the form using form.Controls["NomeComponente"] .

And then only call the method in Form passing this , which would be the form itself.

    
03.08.2015 / 12:44
2

The FillInterface function could receive the form object ( link ) and iterate over the collection of controls within the form ( ).

private void button_t50_carregar_Click(object sender, EventArgs e)
    {
        // Obtém o formulário do botão clicado
        // (supondo que o form é o controle pai imediato do botão
        Form formulario = (Form)((sender as Button).Parent)

        // Código de ler o dado

        // Usa uma função para preencher os dados
        T50_Data.PreencheInterface(formulario);
    }
    
01.08.2015 / 22:51
1

I was able to solve the problem by gathering everyone's information. Thanks to @MyChapeu and @rocmartins. I'll leave my solution here for future questions.

The problem was that my Form1 is made up of a TapPage, inside another TabPage, and some buttons are still inside a GroupBox. From this button to return to Form1, I had to do:

Form formulario = (Form)((((((((((sender as Button).Parent) as TabPage).Parent) as TabControl).Parent) as TabPage).Parent) as TabControl).Parent);

So I can pass this "form" to my method. And to access a button that is inside a GroupBox that is in this Tabpage:

form.Controls["TabControl1"].Controls["TabPage1"].Controls["TabControl2"].Controls["groupBox1"].Controls["button"].Text = "Novo Texto Botão".

Thanks for the help!

    
05.08.2015 / 21:39