gpt4 book ai didi

c# - .net cf TextBox 显示键盘焦点

转载 作者:太空狗 更新时间:2023-10-29 23:59:36 25 4
gpt4 key购买 nike

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

关于c# - .net cf TextBox 显示键盘焦点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/967281/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com