Reputation: 67
I have made a button to navigate from tab to tab, I'm wanting to create a navigational program which looks as if I am navigating from room to room, how can I disable the use of Ctrl+Tab & Ctrl+Shift+Tab from my App Users in Tab Control within Visual Studios 2015?
Upvotes: 1
Views: 93
Reputation: 941217
Keyboard handling for TabControl is fairly unusual, it raises the KeyDown event even if a child control has the focus. But setting e.Handled = true does not suppress the keystroke. Bit of a bug. Two basic ways to work around this restriction, they both involve overriding the protected ProcessCmdKey() method. First one is to derive your own class from TabControl, usually desirable if you want to tinker with the control for other reasons. Like this:
using System;
using System.Windows.Forms;
public class MyTabControl : TabControl {
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == (Keys.Tab | Keys.Control) ||
keyData == (Keys.Tab | Keys.Control | Keys.Shift)) {
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
Build your program and drop the new control from the top of the toolbox, replacing the existing one.
The other way to do it is to intercept the keystroke before it can reach the tab control. More of a slog since you have to pay attention to which control has the focus and whether or not it is a child of the tab control, copy/paste this code into your form class:
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
var ctl = this.ActiveControl;
while (ctl != null) {
if (ctl == myTabControl1) {
if (keyData == (Keys.Tab | Keys.Control) ||
keyData == (Keys.Tab | Keys.Control | Keys.Shift)) {
return true;
}
}
ctl = ctl.Parent;
}
return base.ProcessCmdKey(ref msg, keyData);
}
Replace "tabControl1" with the name of your control.
Upvotes: 1