简体   繁体   中英

.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.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM