I have a TabControl
in my form which contains 5 tabs. I want the focus to goes to the next tab, when I press TAB key. I have set the TabIndex
property of each TabPage
control. But it doesn't work.
I also tried this code for the RichTextBox
I have in my TabPages
.
private void rchFisrtTab_KeyDown(object sender, KeyEventArgs e)
{
if (e.KeyCode == Keys.Tab)
{
tabText.SelectedTab = tbpSecondTab;
rchSecondTab.Focus();
rchSecondTab.Select();
}
}
But it doesn't get triggered at all! So I'm wondering how can I make it work.
Thank you.
You need to intercept the TAB
key by subclassing the TabControl
and overriding one of the ProcessXXX
methods, eg
class FormTab2 : Form {
public FormTab2() {
TabControl tc = new TC2 { Dock = DockStyle.Fill };
TabPage p1 = new TabPage() { Text = "Tab1" };
TabPage p2 = new TabPage() { Text = "Tab2" };
p1.Controls.Add(new Button { Text = "Button1" });
p2.Controls.Add(new Button { Text = "Button2" });
tc.TabPages.Add(p1);
tc.TabPages.Add(p2);
Controls.Add(tc);
}
public class TC2 : TabControl {
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Tab && this.Focused) {
int x = (SelectedIndex + 1) % TabPages.Count;
SelectedTab = TabPages[x];
//this.Focus();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
}
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.