C#:在表单上使用 TextPreview 时,TextBox 未接收键
我正在用 C# 在 Windows 窗体中实现搜索功能。我已在表单上将 KeyPreview
设置为 true,并为 KeyDown
添加了一个事件处理程序,这样我就可以捕获 ctrl+f
、等内容>esc
并输入
。
我很好地抓住了这些键,并且能够显示我的文本框,但我无法在框中输入内容。所有键都将发送至 PortsTraceForm_KeyDown(...)
,但它们从未到达文本框。根据有关 KeyPreview
的 msdn 页面,将 e.Handled 设置为 false 应该会导致事件传递到焦点视图(文本框),但这并没有发生。我尚未为文本框注册 KeyDown
事件,因此它应该使用默认行为。我错过了什么吗?
KeyDown 事件:
private void PortsTraceForm_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
e.Handled = false;
if (e.KeyData == (Keys.F | Keys.Control)) // ctrl+f
{
e.Handled = true;
ShowSearchBar();
}
else if (e.KeyCode == Keys.Escape) // esc
{
e.Handled = true;
HideSearchBar();
}
else if (e.KeyCode == Keys.Enter) // enter
{
if (searchPanel.Visible)
{
e.Handled = true;
if (searchShouldClear)
SearchStart();
else
SearchNext();
}
}
}
显示搜索栏:
private void ShowSearchBar()
{
FindBox.Visible = true;
FindBox.Focus(); // focus on text box
}
隐藏搜索栏:
private void HideSearchBar()
{
this.Focus(); // focus on form
FindBox.Visible = false;
}
I am implementing a search function in a windows form in c#. I have set KeyPreview
to true on the form and have added an event handler for KeyDown
so I can catch things like ctrl+f
, esc
and enter
.
I am catching these keys just fine and I'm able to make my text box appear, but I am unable to type into the box. All of the keys are going to PortsTraceForm_KeyDown(...)
but they never make it to the text box. According to the msdn page about KeyPreview
, setting e.Handled to false should cause the event to pass to the view in focus (the text box), but this isn't happening. I have not registered a KeyDown
event for the text box, so it should be using the default behavior. Have I missed something?
KeyDown event:
private void PortsTraceForm_KeyDown(object sender, KeyEventArgs e)
{
e.SuppressKeyPress = true;
e.Handled = false;
if (e.KeyData == (Keys.F | Keys.Control)) // ctrl+f
{
e.Handled = true;
ShowSearchBar();
}
else if (e.KeyCode == Keys.Escape) // esc
{
e.Handled = true;
HideSearchBar();
}
else if (e.KeyCode == Keys.Enter) // enter
{
if (searchPanel.Visible)
{
e.Handled = true;
if (searchShouldClear)
SearchStart();
else
SearchNext();
}
}
}
show search bar:
private void ShowSearchBar()
{
FindBox.Visible = true;
FindBox.Focus(); // focus on text box
}
hide search bar:
private void HideSearchBar()
{
this.Focus(); // focus on form
FindBox.Visible = false;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
即使您正在调用
Focus()
,您的 TextBox 也可能没有焦点。来自文档:您可以检查 Focus() 的返回值是否成功,但我过去使用该方法将焦点设置到任意控件时运气不佳。相反,请尝试使用文档建议的方法,即调用
Select()
。编辑:
没关系(尽管它仍然是有效的建议),我想我看到你的问题:
你为什么这样做?再次,来自文档:
。因此,您有意阻止 TextBox 获取按键事件。如果您想传递事件,则不应将该属性设置为
false
。Your TextBox likely does not have focus even though you are calling
Focus()
. From the documentation:You can check the return value of
Focus()
for success, but I have had little luck in the past using that method to set focus to an arbitrary control. Instead, try using the method that the documentation suggests, i.e., callSelect()
.EDIT:
Nevermind (though it's still valid advice), I think I see your problem:
Why are you doing this? Again, from the docs:
So you are intentionally preventing the TextBox from getting key events. If you want to pass the event through you shouldn't be setting that property to
false
.尝试这个例子,覆盖方法。
问候。
try this example , of overrides method.
Regards.