2012-03-26 4 views
0

두 개의 사용자 지정 컨트롤을 만들었습니다. 기능 중 하나를 선택하여 C# 응용 프로그램에서 사용할 수 있습니다. 원하는 컨트롤을로드했는데 어떻게 사용할 수 있습니까? 예 : 내 컨트롤 LoadXML()의 공용 함수가 있습니다. 두 컨트롤 모두이 기능을 포함합니다. 한 번에 하나의 컨트롤 만로드됩니다.C# 응용 프로그램에 동적으로 사용자 지정 컨트롤 추가

+0

어떻게로드합니까? 그들이 어디에 적재되어 있는지 아십니까? 언제 LoadXML()을 호출해야합니까? –

답변

0

컨트롤의 인스턴스를 만든 다음 양식에 추가하면 공용 노출 메서드를 호출 할 수 있습니다.

TestControl myTestControl = new TestControl(); 
this.Controls.Add(myTestControl); 
myTestControl.LoadXML(); 

당신이 DLL을 통해 컨트롤을로드하는 경우, 호출하는 방법이 시도 : 내가 제대로 질문을 이해 한 경우가 인터페이스를 생성하고 추가한다

// Use the file name to load the assembly into the current 
    // application domain. 
    Assembly a = Assembly.Load("example"); 
    // Get the type to use. 
    Type myType = a.GetType("Example"); 
    // Get the method to call. 
    MethodInfo myMethod = myType.GetMethod("MethodA"); 
    // Create an instance. 
    object obj = Activator.CreateInstance(myType); 
    // Execute the method. 
    myMethod.Invoke(obj, null); 

http://msdn.microsoft.com/en-us/library/25y1ya39.aspx

+0

myassembly = Assembly.LoadFrom (Application.StartupPath + "\\ PDF"+ "\\ x86"+ "\\ PDFView"+ ".dll"); } 경우 (하여 MyAssembly = 널!) { 유형 t = myassembly.GetType ("PDFView.PDFViewer"); cc = (Control) Activator.CreateInstance (t); this.Controls.Add (cc); cc.Dock = DockStyle.Bottom; cc.Show(); – abdul

+0

두 컨트롤의 .dll에서로드 중입니다. 응용 프로그램은 .dll이로드 될 런타임을 결정합니다. – abdul

+0

cc = (Control) Activator.CreateInstance (t); 여기에 컨트롤 – Habib

0

을 기능이 LoadXML()입니다. 사용자 정의 컨트롤에 인터페이스를 구현하십시오. 이제 인터페이스 개체를 만들고 원하는 컨트롤로 초기화 할 수 있습니다. UserControl2 는 이제 인터페이스 객체에 원하는 사용자 컨트롤을로드하고 LoadXML() 전화에 대한 사용자 제어에

interface MyInterface 
    { 
     void LoadXML(); 
    } 

,
class Class 
    { 
     MyInterface control; 

     public Class() 
     { 
      if (condition == true) 
       control = new UserControl1(); 
      else 
       control = new UserControl2(); 

      control.LoadXML(); 
     } 
    } 

는 희망이 도움이 될 것입니다, MyInterface

public class UserControl1 : UserControl, MyInterface 
    { 
     public void LoadXML() 
     { 
      ... //do what you want 
     } 
    } 

같은 구현 .

관련 문제