2012-12-21 101 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 
     } 
    } 

// 사용자 정의를 perfom이 정기

 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

지금은 데이터를 표시하기 위해 여는 다른 하위 폼을 가질 수있는 하나의 마스터 하위 폼에 있습니다. 그러나 당신이 언급 했으므로 다른 사람들과 약간의 유사점이있을 수 있습니다. – John

+0

나는 여전히 그 질문을 잘 모른다. 그러나 비행에 대한 추측은 상속으로 문제를 해결할 수 있습니까 ?? 나는 일반적인 기능을 가진 클래스를 만들고 그 클래스의 나머지 클래스를 파생시킨다. 공통 클래스에는 버튼과 이벤트가 포함될 수도 있습니다. – Sandy

답변

1

에 모든 버튼 오프닝 통합 함수 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 
     } 
    } 
관련 문제