.NET CF文本框的焦点显示键盘 [英] .net cf TextBox that displays keyboard on focus

查看:133
本文介绍了.NET CF文本框的焦点显示键盘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我想要显示的手机键盘当控件具有专注于我的UI几个文本框,然后走开。

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.

推荐答案

添加一个输入面板到您的形式,挂钩的文本框,并显示的GotFocus和LostFocus事件/隐藏输入面板,在事件处理程序:

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

更新

在响应于对完整性ctacke的请求;下面是示例$ C $下挂钩的事件处理程序。通常我会使用设计这个(选择文本框,显示属性网格,切换到事件列表,并在环境设置处理程序的GotFocus 引发LostFocus ),但如果UI包含超过几个文本框,你可能希望它更多的自动化。

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.

下面类有两个静态方法,AttachGotLostFocusEvents和DetachGotLostFocusEvents;他们接受的ControlCollection和两个事件处理程序。

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

这篇关于.NET CF文本框的焦点显示键盘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆