Question

i have an MDIParent and a Menustrip so when i click on a StripMenuitem shows me another form inside my MdiParent Form, so my problem is : the Form_Load Event for the form which is opened inside the MdiParent wont work !,it seems like i have to use another event :/

Any idea? Thank you

here is the code how i show my form inside the MdiParent form

FormVehicule FV;
private void véhiculeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (FV == null)
            {
                FV = new FormVehicule();
                FV.MdiParent = this;
                FV.WindowState = FormWindowState.Maximized;
                FV.Show();
            }
            else
            {
                FV.WindowState = FormWindowState.Maximized;
                FV.Show();
                FV.BringToFront();
            }
        }

So in the code of the child Form FormVehicule

private void FormVehicule_Load(object sender, EventArgs e)
        {
            comboBoxUnite.SelectedIndex = 0;
            U = new Unite(FormLogin.Con);
            U.Lister();
            for (int i = 0; i < U.C.Dt.Rows.Count; i++)
                comboBoxUnite.Items.Add(U.C.Dt.Rows[i][0].ToString());
            comboBoxConducteur.SelectedIndex = 0;
            C = new Conducteur(FormLogin.Con);
            C.Lister();
            for (int i = 0; i < C.C.Dt.Rows.Count; i++)
                comboBoxConducteur.Items.Add(C.C.Dt.Rows[i][0].ToString());
            V = new Vehicule(FormLogin.Con);
            V.Lister();
            dataGridViewVehicule.DataSource = V.C.Dt;
            MessageBox.Show("Test");
        }
Was it helpful?

Solution

How do you handle the Form.Load event?

The same code works to me:

void toolStripMenuItem1_Click(object sender, EventArgs e) {
    Form childForm = new Form();
    childForm.MdiParent = this;
    childForm.Load += childForm_Load; // subscribe the Form.Load event before Form.Show()
    childForm.Show(); // event will be raised from here
}
void childForm_Load(object sender, EventArgs e) {
    // ...
}

You can also use the following approach:

void toolStripMenuItem1_Click(object sender, EventArgs e) {
    MyChildForm form = new MyChildForm();
    form.MdiParent = this;
    form.Show();
}
class MyChildForm : Form {
    protected override void OnLoad(EventArgs e) {
        base.OnLoad(e);
        //...
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top