代码之家  ›  专栏  ›  技术社区  ›  Chris Brandsma

.net cf文本框,在焦点上显示键盘

  •  4
  • Chris Brandsma  · 技术社区  · 15 年前

    注意:对于这个特定的程序,它是一个高屏幕,设备上没有物理键盘。

    2 回复  |  直到 15 年前
        1
  •  13
  •   Fredrik Mörk    15 年前

    在表单中添加InputPanel,连接文本框的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;
    }
    

    使现代化

    GotFocus LostFocus

    下面的类公开了两个静态方法,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();
    }
    
        2
  •  5
  •   ctacke    15 年前

    使用 InputPanel class . Enable 当你获得焦点时,它会被激活;当你失去焦点时,它会被禁用。