0

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");
        }
Obama
  • 2,586
  • 2
  • 30
  • 49

1 Answers1

2

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);
        //...
    }
}
DmitryG
  • 17,677
  • 1
  • 30
  • 53
  • Hello, this solution work in the same form , but me i have a Form inside an MdiParent form so i want the Form_Load of the child not the MdiParent , Thanks! – Obama Feb 17 '12 at 15:08
  • i show My MessageBox test, but after it shows me a blank form(ChildForm) :/ – Obama Feb 17 '12 at 15:32
  • @Abdel: Does the approach with OnLoad works? The test message box is shown - so, the question is resolved? To resolve your another problem with a blank form please provide more details (in new question-thread). It is not quite clear what the exactly you expected without the full source code... – DmitryG Feb 17 '12 at 15:45