When using the constructor when using load

Asked

Viewed 198 times

0

Good night, 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();
    }

1 answer

2


The answer is depends on. It all depends, always.

You should know which is the best place to place the loading of the components or whatever you intend to do. What you accurate even knowing is when each one is shot.

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.

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

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

Ex.:

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

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

Usually, to call a form, one 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
 
  • Ha perfect friend, I thought it had type definitions, in the constructor and used to declare variables for example. but blz, I got it, and I’m gonna study the case. Thanks

Browser other questions tagged

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