2012-02-17 44 views
0

我有一個MDIParent和一個Menustrip,所以當我點擊一個StripMenuitem時,顯示了我的另一個窗體,我的MdiParent窗體, 所以我的問題是:Form_Load事件在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(); 
      } 
     } 

所以在子窗體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"); 
     } 
+1

您可以加入一些代碼片段?技術上,如果你使用'WinForm.Show()',它應該在技術上觸發'Form_Load'事件 – Alexandre 2012-02-17 14:52:27

+0

你想調用Form_Load事件.. ??如果是這樣,只是把事件名稱(發件人,電子),如果這是你在說什麼,否則改寫你的問題,使其更清晰 – MethodMan 2012-02-17 14:55:35

+0

現在,這裏是代碼:) – Obama 2012-02-17 14:56:32

回答

2

你是如何處理表單的代碼在我的形式。加載事件?

相同的代碼工作對我說:

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); 
     //... 
    } 
} 
+0

你好,這個解決方案工作在相同的形式,但我我有一個MdiParent形式內的窗體,所以我想FormMLoad的孩子不是MdiParent,謝謝! – Obama 2012-02-17 15:08:11

+0

@Abdel:看到我的更新 – DmitryG 2012-02-17 15:19:20

+0

我顯示了我的MessageBox測試,但是在它顯示了一個空白表單(ChildForm)之後:/ – Obama 2012-02-17 15:32:24

相關問題