我有一个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();
}
}
所以在子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");
}
答案 0 :(得分: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);
//...
}
}