2009-06-08 3 views

答えて

13

イベントハンドラで、入力パネルを非表示/テキストボックスとショーののGotFocusとのLostFocusイベントをフックアップ、フォームに入力パネルを追加します。完全性のためのctackeの要求。ここにイベントハンドラを接続するためのサンプルコードを示します。通常、私はこれにデザイナを使用します(テキストボックスの選択、プロパティグリッドの表示、イベントリストへの切り替え、環境設定のハンドラの設定はGotFocusLostFocus)、UIには複数のテキストボックスが含まれている場合は、それをより自動化する。

次のクラスは、2つの静的メソッド、AttachGotLostFocusEventsおよびDetachGotLostFocusEventsを公開します。彼らはControlCollectionと2つのイベントハンドラを受け入れます。フォームで

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; 
} 

更新

関連する問題