Pass Controls to a function

Asked

Viewed 329 times

2

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

I have already implemented a button that saves the object created by the T50 class. I can save and read this object (Using serialize).

Only when I re-read a saved object I need to fill in the part of the interface that the user had already filled in.

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 receive them individually, for example

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

There’s a way I can pass them all at once?

  • You’re using WPF or Windows Forms ?

  • I am using Windows Forms. But my knowledge is not very advanced.

3 answers

3


You could do it like this:

Pass your Form for the method:

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

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

And then just call the method on Form passing the this, which would be the form itself.

2

The Fillinterface function could receive the form object (https://msdn.microsoft.com/pt-br/library/system.windows.forms.form(v=vs.110). aspx) and iterate on the collection of controls within the form (https://msdn.microsoft.com/pt-br/library/4z8d1tx5(v=vs.110). aspx).

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);
    }
  • Is it the object of the whole of Form1? But how do I pass this object to it. That’s what I’m not getting.

  • You can use the "Parent" attribute of the button (until you reach the form) and cast your cast for the form type. I improved the answer.

1

I was able to solve the problem by gathering everyone’s information. Thanks to @Meuchapeu and @rocmartins. I will leave my solution here for future doubts.

The problem was that my Form1 is composed by a Tappage, inside another Tabpage, and some buttons are still inside a Groupbox. From that button to get back 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!

Browser other questions tagged

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