.net cf TextBox that displays keyboard on focus

后端 未结 2 968
盖世英雄少女心
盖世英雄少女心 2021-01-03 06:06

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

相关标签:
2条回答
  • 2021-01-03 06:44

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

    0 讨论(0)
  • 2021-01-03 07:01

    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();
    }
    
    0 讨论(0)
提交回复
热议问题