如何防止选项卡在被选中时呈现?
我希望能够让用户能够浏览选项卡,将焦点设置到每个选项卡,但只有当他们按 Enter 时,选项卡页面才会呈现。
您可能认为会涉及绘画事件,但我不知道如何“取消”它,如果这能完成工作的话。
I would like to be able to have a user be able run through the tabs, setting focus to each one, but only when they hit enter, the tabpage will render.
You would think that the paint event would be involved, but I don't know how to "cancel out" of it, if that would even do the job..
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,我应该提醒您,您正在覆盖标准 Windows 行为。在任何属性页对话框或用户界面中使用选项卡的任何其他地方,使用向左和向右箭头键将翻转选项卡,并使它们在选项卡控件中显示其内容。您不必按 Enter 键即可显示选定的选项卡页。如果您决定走这条路,请确保您的用户了解您的应用程序是不同的(并且您了解用户的需求)。
也就是说,您可以通过处理
KeyDown
事件,检测何时按下其中一个箭头键,然后取消它。 例如:但是,一旦这样做,用户将无法将焦点设置到特定选项卡页的选项卡,因为一旦该选项卡获得焦点,该选项卡页就会立即进入视图。这是由父
TabControl
处理的,与Paint
事件无关(该事件负责控件如何绘制,而不是何时或为何绘制)。当然,您始终可以确定是否在同一个
KeyDown
事件中按下了 Enter 键,并激活您希望的任何选项卡页(例如,通过使用每次相应的时间递增/递减的计数器变量)按下箭头键),但不会向用户显示哪个选项卡将进入视图。不会绘制焦点矩形。另请注意,按 Ctrl+Tab 或 Ctrl+Page Up/Page Down 将在标签页之间切换。如果这也是不可取的,那么您还需要注意并取消这些组合键。
任何时候您开始尝试覆盖默认行为,您都会遇到更多麻烦而不是围绕它设计应用程序。如果出于特殊原因您需要使用 Enter 键来进行选项卡页切换,我们也许能够帮助您提出更简单、更好的解决方案。
First, I should caution you that you're overriding the standard Windows behavior. In any property page dialog or anywhere else that uses tabs in the user interface, using the left and right arrow keys will flip through the tabs and cause them to display their contents in the tab control. You do not have to press Enter to get the selected tab page to display. Make sure that your users understand that your application is different (and that you understand the needs of your users) if you decide to go this route.
That said, you can override this behavior by handling the
KeyDown
event for theTabControl
, detecting when one of the arrow keys has been pressed, and cancelling it. For example:However, once you do this, there will be no way for the user to set focus to a particular tab page's tab, because once the tab gets focus, the tab page is immediately brought into view. This is handled by the parent
TabControl
and is unrelated to thePaint
event (which is responsible for how the control gets painted, not when or why).Of course, you can always determine if the Enter key was pressed in the same
KeyDown
event and activate any tab page that you wish (such as by using a counter variable that is incremented/ decremented each time the corresponding arrow key is pressed), but there will be no visible indication to the user which tab will then be brought into view. The focus rectangle will not be drawn.Also be aware that pressing Ctrl+Tab or Ctrl+Page Up/Page Down will switch between tab pages. If this is also undesirable, you'll need to watch for and cancel these key combinations as well.
Any time you start trying to override default behaviors, you're in for a lot more trouble than if you just design your application around it. If there's a particular reason you want to require the Enter key to commit tab page switching, we might be able to help you come up with an easier and better solution.
我不确定我是否理解您想要完成的任务,但听起来您可以使用
Visible
属性来完成此任务。当用户切换到 TabPage 时,您应该能够将 TabPage 的可见性设置为 false,然后仅在需要时将其设置为 true。
I'm not sure I understand what you are trying to accomplish, but it sounds like you can do it using the
Visible
property.You should be able to set the TabPage's visibility to false when the user switches to it, and then set it to true only when you want to.