2011-12-11 4 views
1

컨트롤에 포커스가 사용자 정의 컨트롤에 있는지 여부를 감지 할 수 있습니까? 사용자 정의 컨트롤을 디자인 타임에 추가하는 컨트롤이 아니라 폼에서 사용자 정의 컨트롤을 사용하여 추가 한 컨트롤을 의미합니다. 평균적인 예는 패널입니다. 내 사용자 정의 컨트롤은 패널처럼 작동하며 내 컨트롤에 포함 된 (중첩 된) 컨트롤에 포커스가있을 때이를 감지하고 싶습니다.사용자 정의 컨트롤 내부 컨트롤 포커스 검색

감사합니다.

답변

1

내가 접근 할 방법은 UserControl이 만들어지고 디자인 모드에 있지 않을 때 사용자 컨트롤 내의 각 컨트롤을 순환하여 GotFocus 이벤트에 후크를 추가하고 해당 메서드에 대한 후크를 가리키는 것입니다. UserControl (ChildControlGotFocus)은 사용자 정의 컨트롤의 호스트에서 사용할 수있는 이벤트를 발생시킵니다.

public partial class UserControl1 : UserControl 
{ 
    public UserControl1() 
    { 
     InitializeComponent(); 

     if (!this.DesignMode) 
     { 
      RegisterControls(this.Controls); 
     } 

    } 
    public event EventHandler ChildControlGotFocus; 

    private void RegisterControls(ControlCollection cControls) 
    { 
     foreach (Control oControl in cControls) 
     { 
      oControl.GotFocus += new EventHandler(oControl_GotFocus); 
      if (oControl.HasChildren) 
      { 
       RegisterControls(oControl.Controls); 
      } 
     } 
    } 

    void oControl_GotFocus(object sender, EventArgs e) 
    { 
     if (ChildControlGotFocus != null) 
     { 
      ChildControlGotFocus(this, new EventArgs()); 
     } 
    } 
} 
+0

감사 :

예를 들어, 여기에이 기능을 구현하는 샘플의 UserControl입니다. 네가 한 일에 정말 감사한다. – MahanGM