0

我有一个 MDIParent 和一个 Menustrip,所以当我单击 StripMenuitem 时,我会在我的 MdiParent 表单中显示另一个表单,所以我的问题是:在 MdiParent 中打开的表单的 Form_Load 事件不起作用!,似乎我必须使用另一个事件:/

任何的想法?谢谢

这是我如何在 MdiParent 表单中显示我的表单的代码

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

所以在子FormFormVehicule的代码中

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");
        }
4

1 回答 1

2

你如何处理 Form.Load 事件?

相同的代码对我有用:

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) {
    // ...
}

您还可以使用以下方法:

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);
        //...
    }
}
于 2012-02-17T15:02:11.363 回答