简体   繁体   English

.net cf在焦点上显示键盘的TextBox

[英].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. 我的UI上有一些文本框,我想在控件有焦点时显示移动键盘,然后消失。

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: 将InputPanel添加到表单,连接TextBox的GotFocus和LostFocus事件,并在事件处理程序中显示/隐藏输入面板:

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; 回应ctacke的完整性要求; 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. 通常我会使用设计器(选择文本框,显示属性网格,切换到事件列表并为GotFocusLostFocus设置环境),但如果UI包含多个文本框,您可能希望让它更自动化。

The following class exposes two static methods, AttachGotLostFocusEvents and DetachGotLostFocusEvents; 下面的类公开了两个静态方法,AttachGotLostFocusEvents和DetachGotLostFocusEvents; they accept a ControlCollection and two event handlers. 他们接受ControlCollection和两个事件处理程序。

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 . 使用InputPanel类 Enable it when you get focus, disable it when you lose focus. 在获得焦点时启用它,在失去焦点时禁用它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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