0

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

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
KevinDeus
  • 11,988
  • 20
  • 65
  • 97

2 Answers2

1

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • Awoseme.. Thank you. Believe it or not I was trying to replicate windows tab behavior used for browser windows in IE and Chrome. I have a tab with a '+' on it that I use to add tabs, just like in the browser. (I guess I could create an "add" button and float it next to the tab, but I think it makes it easier to code). I wound up making 'currentTab' and 'previousTab' variables and switching them out on the tabControl_Selected event. (a variation on this: http://efreedom.com/Question/1-1323470/Tell-Tab-Moving-NET-Tab-Control) – KevinDeus Dec 02 '10 at 22:53
  • oh yeah. Also, being cognizant of key behavior, my method replicates what the browsers do (if you notice, you can't tab to the '+' in the browser (probably because its a button) – KevinDeus Dec 02 '10 at 22:55
  • @KevinDeus: Ah, well if it's only the "new tab" tab that you're worried about, that would make solving the problem a lot easier. You can still retain the default behavior for your *actual* tabs, while overriding this behavior only when the "new tab" tab-that-should-be-a-button is involved. (Also, it wouldn't be *that* difficult to custom draw the tab control and add a button to the far right side, if you were interested, which is what IE/Chrome do.) Anyway, glad this worked for you, and I feel a lot better about your design after your explanation. :-) – Cody Gray - on strike Dec 03 '10 at 10:14
0

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.

Ran
  • 5,989
  • 1
  • 24
  • 26