2012-12-21 49 views
2

我發現我開始對幾個不同的點擊事件使用相同的代碼。我有一個MDI表格,當我打電話給他們時,會有「主子女」,這將打開與主人關聯的其他子女。這是主要/細節的事情。一個例子是公司主人的孩子將有按鈕來打開與公司有關的聯繫人,行業等。以下是打開Contact子表單的代碼示例。此代碼也被其他人使用。需要統一子窗體按鈕點擊成一個例程

我期望做的是能夠使用一個,並填寫公司和聯繫人之間的按鈕,表單,消息和連接標籤。 botton上的代碼就是我目前爲止的內容,並且標記了需要隨我所尋找的內容而改變的行。帶有單箭頭的線似乎「可以」工作,但多箭頭線無法正確顯示。提供兩種用於比較的原因。

有人可以看看這/這些,看看我在做什麼錯誤(或缺少)在整合的代碼?

謝謝...約翰

//代碼,打開聯繫人子窗體

 private void btnCompanyContact_Click(object sender, EventArgs e) 
    { 
     bool isOpen = false; 

     foreach (Form f in Application.OpenForms) 
     { 
      if (f is frmContact) 
      { 
       isOpen = true; 
       MessageBox.Show("The Contact list is already open.", "INFORMATION", MessageBoxButtons.OK); 
       f.BringToFront(); 
       f.Controls["lblRecordID"].Text = lblCompanyID.Text; 
       break; 
      } 
     } 

     if (!isOpen) 
     { 
      frmContact contact = new frmContact(); 
      contact.MdiParent = this.MdiParent; 
      contact.ReceiveValue(lblCompanyID.Text); 
      contact.StartPosition = FormStartPosition.Manual; 
      contact.Location = new Point(100, 100); 
      contact.Show(); 
     } 

     else 
     { 
      //do nothing 
     } 
    } 

//合併所有按鈕開口進入這個程序

 private void OpenCompanyInformationForm(Button btn, Form name, string message, string lbl) 
    { 
     bool isOpen = false; 

     foreach (Form f in Application.OpenForms) 
     { 
    ->  if (f == name) 
      { 
       isOpen = true; 
    ->   MessageBox.Show("The " + message + " list is already open.", "INFORMATION", MessageBoxButtons.OK); 
       f.BringToFront(); 
    ->   f.Controls[lbl].Text = lblCompanyID.Text; 
       break; 
      } 
     } 

     if (!isOpen) 
     { 
    ->->-> frmContact contact = new frmContact(); 
      contact.MdiParent = this.MdiParent; 
      contact.ReceiveValue(lblCompanyID.Text); 
      contact.StartPosition = FormStartPosition.Manual; 
      contact.Location = new Point(100, 100); 
      contact.Show(); 
     } 

     else 
     { 
      //do nothing 
     } 
    } 
+0

你只是試圖在所有形式中重複使用相同的代碼? – Sandy

+0

現在它只是在一個主子表單中,將有6個其他子表單打開以顯示數據。但是既然你提到它,其他人可能會有一些相似之處。 – John

+0

我仍然不確定這個問題。但在飛行中的猜測是,可以繼承解決你的問題?我的意思是創建一個具有通用功能的類,並從中派生出其餘的類。你的普通班級甚至可以包含按鈕及其事件。 – Sandy

回答

1

要perfom定製函數ReceiveValue您需要從表格 創建派生類並創建此派生類中的所有表單

public class ContactBase : Form 
{ 

    public void ReceiveValue(string p_Value) 
    { 
     Button button = (Button)this.Controls["lblRecordID"]; 
     if (button == null) return; 
     button.Text = p_Value; 
    } 

} 


private void OpenCompanyInformationForm(Form name) 
    { 
     bool isOpen = false; 

     foreach (Form f in Application.OpenForms) 
     { 
      // Just to compare, you can use the Name property 
    ->  if (f.Name == name.Name) 
      { 
       isOpen = true; 
       // If the message is just a name of form, you can use Name or Text property 
       // in this case you can supress message param 
    ->   MessageBox.Show("The " + f.Text + " list is already open.", "INFORMATION", MessageBoxButtons.OK); 
       f.BringToFront(); 
       // If the ReceiveValue is just to pass the text of lblCompanyID for lblRecordID button, you can use the function here 
    ->   ((ContactBase)name).ReceiveValue(lblCompanyID.Text); 
       break; 
      } 
     } 

     if (!isOpen) 
     { 
    ->->-> ContactBase contact = (ContactBase)Activator.CreateInstance(name.GetType()); 
      contact.MdiParent = this.MdiParent; 
      contact.ReceiveValue(lblCompanyID.Text); 
      contact.StartPosition = FormStartPosition.Manual; 
      contact.Location = new Point(100, 100); 
      contact.Show(); 
     } 

     else 
     { 
      //do nothing 
     } 
    }