When to use the constructor when using load

0
Good evening, how are you? I have a question, when to use the constructor and when to use the load of a form to call some method or etc.

Example:

 public partial class Manutenção_cliente : DevExpress.XtraEditors.XtraForm
{
    consulta_bd consulta_bd = new consulta_bd();
    cadastro_bd cadastro_bd = new cadastro_bd();
    excluir_bd excluir_bd = new excluir_bd();
    controles_text control_text = new controles_text();
    DataTable cobranca = new DataTable();
    DataTable entrega = new DataTable();

    public int id_cliente, editar, chek_excluir, chek_new_cli, contador, id_end_cobranca, id_end_entrega;

    public Manutenção_cliente(int id)
    {
        InitializeComponent();
        this.StartPosition = FormStartPosition.CenterScreen;
        id_cliente = id;
        populargrid_cobranca(id);
        dateTimePicker1.Enabled = false;
    }

    private void lod ()
    {
        textBox13.ReadOnly = true;

        if (id_cliente != 0)
        {
            textBox13.Text = Convert.ToString(id_cliente);
            Dictionary<string, string> preenche_cliente = new Dictionary<string, string>();
            preenche_cliente = consulta_bd.preenche_cliente(id_cliente);

            textBox1.Text = preenche_cliente["razao_social"];
            textBox2.Text = preenche_cliente["nome_fantasia"];
            comboBox1.Text = preenche_cliente["tipo_doc"];
            textBox3.Text = preenche_cliente["n_doc"];
            textBox4.Text = preenche_cliente["ie"];
            textBox5.Text = preenche_cliente["im"];
            textBox6.Text = preenche_cliente["endereco"];
            textBox7.Text = preenche_cliente["numero"];
            textBox8.Text = preenche_cliente["cep"];
            textBox9.Text = preenche_cliente["bairro"];
            textBox10.Text = preenche_cliente["cidade"];
            textBox11.Text = preenche_cliente["estado"];
            textBox12.Text = preenche_cliente["pais"];
            textBox14.Text = preenche_cliente["requisitos"];
            textBox16.Text = preenche_cliente["email"];
            textBox15.Text = preenche_cliente["telefone"];

            int bloq = Convert.ToInt16(preenche_cliente["bloqueado"]);
            if (bloq == 0)
            {
                checkBox1.Checked = false;
            }
            else
            {
                checkBox1.Checked = true;
            }

            foreach (Control ctl in xtraTabPage1.Controls)
            {
                if (ctl is TextEdit)
                {
                    ((TextEdit)(ctl)).ReadOnly = true;
                }

                if (ctl is TextBox)
                {
                    ((TextBox)(ctl)).ReadOnly = true;
                }
            }

            foreach (Control ctl in xtraTabPage3.Controls)
            {
                if (ctl is TextEdit)
                {
                    ((TextEdit)(ctl)).ReadOnly = true;
                }

                if (ctl is TextBox)
                {
                    ((TextBox)(ctl)).ReadOnly = true;
                }
            }

            control_text.textedit_readonly_true(this.Controls);
        }
        else
        {
            int v = consulta_bd.consulta_id_cliente();
            contador = v + 1;

            textBox13.Text = Convert.ToString(contador);
        }
    }

    private void Manutenção_cliente_Load(object sender, EventArgs e)
    {
        lod();
    }
    
asked by anonymous 20.07.2016 / 02:17

1 answer

2

The answer is depends . Everything depends, always.

You should know the best place to load components or whatever you want to do. What you need to even know is when each one is fired.

The form constructor is a normal constructor, so the methods (or instructions in it) will be executed as soon as you instantiate a new form.

The Load event will only be triggered when the form is first shown (when using the Show or ShowDialog method.

There is still the Shown event, this will be triggered every time the form becomes visible (when doing form.Show - after Load - and form.Visible = true ). If you "hide" the form and make it visible again, the Shown event will fire again.

Ex:

public class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    public void Form1_Load(object sender, EventArgs e)
    {
        AlgumMetodo();
    }
}

Generally, to call a form, it does so

 Form1 form = new Form1(); //Chama o construtor e, consequentemente, o InitializeComponent
 form.Show(); //Dispara o Load que, por sua vez, executa AlgumMetodo
 //o Shown será disparado logo após o Load
    
20.07.2016 / 04:34