C# 禁用TAB键
我有这段代码:
this.searchInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.inputKeypress); private void Keypress(object sender, KeyPressEventArgs e) { // If Tab has been pressed if(122 == (int)e.KeyChar) { switchTab(sTab); MessageBox.Show(sTab); } }
它的作用是将焦点设置到另一个元素。 但是,当焦点设置到 TextBox 时,我按 TAB 键,它只会在 TextBox 中创建一个选项卡,并且不会将焦点设置在下一个元素上。
有人知道我怎样才能完成这项工作吗?
我尝试设置 e.Handled = true;但这没有用...
I have this code:
this.searchInput.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.inputKeypress); private void Keypress(object sender, KeyPressEventArgs e) { // If Tab has been pressed if(122 == (int)e.KeyChar) { switchTab(sTab); MessageBox.Show(sTab); } }
What it does is that it sets focus to another element.
But, when the focus is set to a TextBox, and I press TAB, it just makes a tab in the TextBox, and does not set focus on the next element.
Anyone got an idea how can I make this work?
I've tried to set e.Handled = true; but that didn't work...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您是否尝试过将
TextBox
上的AcceptsTab
设置为false
?编辑:
这毫无意义。我运行了一个小型测试应用程序,当
AcceptsTab
和Multiline
时,Tab 键只会将焦点从TextBox
上移开。 > 属性均为true
,无论为KeyPress
定义什么事件处理程序。您确定其他代码没有将
AcceptsTab
设置为true
吗?如果是,将Multiline
设置为false
是否会完全改变选项卡行为?您可以发布更多相关代码吗?Have you tried setting
AcceptsTab
on theTextBox
tofalse
?Edit:
That makes little sense. I ran a small test app, and the tab key only brings focus away from the
TextBox
when itsAcceptsTab
andMultiline
properties are bothtrue
, regardless of an event handler being defined forKeyPress
.Are you sure some other code isn't setting
AcceptsTab
totrue
? If you are, does settingMultiline
tofalse
change the tab behaviour at all? Could you post more of your relevant code?将文本框的
AcceptsTab
属性设置为false
?Set the
AcceptsTab
property of the text box tofalse
?您需要像这样创建一个控件实例,并重写以下方法:
构建解决方案,然后通过在工具箱中的用户控件下找到它,用新的“NoTabTextBox”替换您的常规 TextBox。
这将捕获 Tab 键并强制它不执行任何操作。
You'll need to create an instance of the control like so, and override the following methods:
Build the solution, then subsitute you're regular TextBox with the new one 'NoTabTextBox', by finding it under the user controls in toolbox.
This will capture the Tab key and force it to do nothing.