C# .net cf文本框,在焦点上显示键盘
我的UI上有几个文本框,我想在控件有焦点时显示移动键盘,然后离开C# .net cf文本框,在焦点上显示键盘,c#,.net,compact-framework,C#,.net,Compact Framework,我的UI上有几个文本框,我想在控件有焦点时显示移动键盘,然后离开 注意:对于此特定程序,它是一个高屏幕,设备上没有物理键盘。使用。当您获得焦点时,它会显示,当您失去焦点时,它会禁用。将InputPanel添加到表单中,连接文本框的GotFocus和LostFocus事件,并在事件处理程序中显示/隐藏输入面板: private void TextBox_GotFocus(object sender, EventArgs e) { SetKeyboardVisible(true); } p
注意:对于此特定程序,它是一个高屏幕,设备上没有物理键盘。使用。当您获得焦点时,它会显示,当您失去焦点时,它会禁用。将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;
}
更新
回应ctacke的完整性要求;下面是连接事件处理程序的示例代码。通常我会为此使用设计器(选择文本框,显示属性网格,切换到事件列表,并让环境为GotFocus
和LostFocus
设置处理程序),但如果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();
}
我刚刚读了这篇文章:,他们还建议捕获表单关闭事件。为了完整起见,您可能也应该显示事件的连接。@FredrikMörk伟大的代码,并详细解释了。Thanx很多,一切都适合我,但我还需要一个小东西inputPanel是小键盘怎么能把它改成大键盘