簡體   English   中英

.net cf在焦點上顯示鍵盤的TextBox

[英].net cf TextBox that displays keyboard on focus

我的UI上有一些文本框,我想在控件有焦點時顯示移動鍵盤,然后消失。

注意:對於此特定程序,它是一個高屏幕,設備上沒有物理鍵盤。

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

更新

回應ctacke的完整性要求; 這是連接事件處理程序的示例代碼。 通常我會使用設計器(選擇文本框,顯示屬性網格,切換到事件列表並為GotFocusLostFocus設置環境),但如果UI包含多個文本框,您可能希望讓它更自動化。

下面的類公開了兩個靜態方法,AttachGotLostFocusEvents和DetachGotLostFocusEvents; 他們接受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);
            }
        }
    }
}

表單中的用法示例:

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

使用InputPanel類 在獲得焦點時啟用它,在失去焦點時禁用它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM