Add an InputPanel to your form, include the GotFocus and LostFocus events in the TextBox, and show / hide the input panel in the event handlers:
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; }
Update
In response to a ctacke request for completeness; here is a sample code for connecting event handlers. Usually I would like to use the constructor for this (select a text field, show the property grid, switch to the event list and configure the environment handlers for GotFocus and LostFocus ), but if the user interface contains more than a few texts, the boxes may wish to make it more automated.
The following class provides two static methods: AttachGotLostFocusEvents and DetachGotLostFocusEvents; they accept a ControlCollection and two event handlers.
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); } } } }
Example usage in the form:
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(); }