2009-06-08 7 views

Répondre

13

Ajouter un InputPanel à votre formulaire, brancher les événements GotFocus et LostFocus de la zone de texte et afficher/masquer le panneau d'entrée dans les gestionnaires d'événements:

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

Mise à jour

En réponse à la demande d'exhaustivité de ctacke; Voici un exemple de code pour connecter les gestionnaires d'événements. Normalement, j'utiliserais le concepteur pour cela (sélectionnez la zone de texte, affichez la grille des propriétés, passez à la liste des événements et configurez les gestionnaires d'environnement pour GotFocus et LostFocus), mais si l'interface utilisateur contient plus de quelques zones de texte, vous pouvez souhaiter pour l'avoir plus automatisé.

La classe suivante expose deux méthodes statiques, AttachGotLostFocusEvents et DetachGotLostFocusEvents; ils acceptent un ControlCollection et deux gestionnaires d'événements.

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

Exemple d'utilisation sous une forme:

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

Je viens de lire cet article: http://msdn.microsoft.com/en-us/library/ms838220.aspx, ils recommandent aussi attraper l'événement Form_Closing. –

+0

Bonne information sur la chose Form_Closing là; merci :) –

+0

Pour être complet, vous devriez probablement montrer le câblage des événements aussi. – ctacke

Questions connexes