简体   繁体   中英

How do I prevent a tab from rendering when selected?

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..

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 the TabControl , detecting when one of the arrow keys has been pressed, and cancelling it. For example:

private void myTabControl_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
{
    //Check to see if an arrow key was pressed
    if ((e.KeyCode == Keys.Left) || (e.KeyCode == Keys.Right))
    {
        //Cancel the keypress by indicating it was handled
        e.Handled = true;
    }
}

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 the Paint 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM