2009-06-08 4 views

답변

13

이벤트 처리기의 입력 패널을 숨기/텍스트 상자와 쇼의의 GotFocus 및 LostFocus 이벤트를 연결, 당신의 양식에 InputPanel 추가 완전을위한 ctacke의 요청; 다음은 이벤트 핸들러를 연결하는 샘플 코드입니다. 일반적으로이 디자이너를 사용하여 (텍스트 상자를 선택하고, 속성 표를 표시하고, 이벤트 목록으로 전환하고, 환경을 GotFocusLostFocus에 대한 처리기를 설정하도록했습니다. UI에 몇 개의 텍스트 상자 이상이 포함되어 있다면 그것을 더 자동화시켜야합니다.

다음 클래스는 AttachGotLostFocusEvents 및 DetachGotLostFocusEvents라는 두 가지 정적 메서드를 제공합니다. 이들은 ControlCollection과 두 개의 이벤트 처리기를 허용합니다. 형태

internal static class ControlHelper 
{ 
    private static bool IsGotLostFocusControl(Control ctl) 
    { 
     return ctl.GetType().IsSubclassOf(typeof(TextBoxBase)) || 
      (ctl.GetType() == typeof(ComboBox) && (ctl as ComboBox).DropDownStyle == ComboBoxStyle.DropDown); 
    } 

    public static void AttachGotLostFocusEvents(
     System.Windows.Forms.Control.ControlCollection controls, 
     EventHandler gotFocusEventHandler, 
     EventHandler lostFocusEventHandler) 
    { 
     foreach (Control ctl in controls) 
     { 
      if (IsGotLostFocusControl(ctl)) 
      { 
       ctl.GotFocus += gotFocusEventHandler; 
       ctl.LostFocus += lostFocusEventHandler ; 
      } 
      else if (ctl.Controls.Count > 0) 
      { 
       AttachGotLostFocusEvents(ctl.Controls, gotFocusEventHandler, lostFocusEventHandler); 
      } 
     } 
    } 

    public static void DetachGotLostFocusEvents(
     System.Windows.Forms.Control.ControlCollection controls, 
     EventHandler gotFocusEventHandler, 
     EventHandler lostFocusEventHandler) 
    { 
     foreach (Control ctl in controls) 
     { 
      if (IsGotLostFocusControl(ctl)) 
      { 
       ctl.GotFocus -= gotFocusEventHandler; 
       ctl.LostFocus -= lostFocusEventHandler; 
      } 
      else if (ctl.Controls.Count > 0) 
      { 
       DetachGotLostFocusEvents(ctl.Controls, gotFocusEventHandler, lostFocusEventHandler); 
      } 
     } 
    } 
} 

사용 예 : 난 그냥이 글을 읽고 있었다

private void Form_Load(object sender, EventArgs e) 
{ 
    ControlHelper.AttachGotLostFocusEvents(
     this.Controls, 
     new EventHandler(EditControl_GotFocus), 
     new EventHandler(EditControl_LostFocus)); 
} 

private void Form_Closed(object sender, EventArgs e) 
{ 
    ControlHelper.DetachGotLostFocusEvents(
     this.Controls, 
     new EventHandler(EditControl_GotFocus), 
     new EventHandler(EditControl_LostFocus)); 
} 

private void EditControl_GotFocus(object sender, EventArgs e) 
{ 
    ShowKeyboard(); 
} 

private void EditControl_LostFocus(object sender, EventArgs e) 
{ 
    HideKeyboard(); 
} 
+1

: http://msdn.microsoft.com/en-us/library/ms838220.aspx을, 그들은 또한 잡기 추천 Form_Closing 이벤트. –

+0

Form_Closing에 좋은 정보가 있습니다. 감사합니다 :) –

+0

완벽을 위해 당신은 아마 너무 이벤트를 배선해야합니다. – ctacke

5

InputPanel class을 사용하십시오. Enable 초점을 맞출 때 초점을 잃을 때 해제하십시오. 에 대한 응답으로

private void TextBox_GotFocus(object sender, EventArgs e) 
{ 
    SetKeyboardVisible(true); 
} 

private void TextBox_LostFocus(object sender, EventArgs e) 
{ 
    SetKeyboardVisible(false); 
} 

protected void SetKeyboardVisible(bool isVisible) 
{ 
    inputPanel.Enabled = isVisible; 
} 

업데이트

:

관련 문제