Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.net cf TextBox that displays keyboard on focus

I have a few text boxes on my UI that I want to display the mobile keyboard for when the control has focus, then go away.

Note: for this particular program, it is a tall screen and has no physical keyboard on the device.

like image 201
Chris Brandsma Avatar asked Dec 03 '22 08:12

Chris Brandsma


2 Answers

Add an InputPanel to your Form, hook up the GotFocus and LostFocus events of 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 ctacke's request for completeness; here is sample code for hooking up the event handlers. Normally I would use the designer for this (select the textbox, show the property grid, switch to the event list and have the environment set up handlers for GotFocus and LostFocus), but if the UI contains more than a few text boxes you may wish to have it more automated.

The following class exposes 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);
            }
        }
    }
}

Usage example in a 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();
}
like image 93
Fredrik Mörk Avatar answered Dec 04 '22 21:12

Fredrik Mörk


Use the InputPanel class. Enable it when you get focus, disable it when you lose focus.

like image 26
ctacke Avatar answered Dec 04 '22 22:12

ctacke