I have an tabControl1 in my form with three TabPages named TabPage1, TabPage2 and TabPage3.
When TabPage 2 has focus I need to raise an key event (arrow keys for navigation). This event should not be raised in the other TabPages.
Anybody know how?
I have an tabControl1 in my form with three TabPages named TabPage1, TabPage2 and TabPage3.
When TabPage 2 has focus I need to raise an key event (arrow keys for navigation). This event should not be raised in the other TabPages.
Anybody know how?
On Selected event handler you can cast the sender to the proper control and check for it's name. If the event is generated from TabPage2 you can fire the key event.
Something like this
private void TabPage_Selected(object sender, EventArgs e)
{
TabPage source = sender as TabPage;
if(source.Name.equals("TabPage2"))
//Do whatever...
}
You'll need to derive your own control from TabControl so that you can intercept the arrow keys and generate an event. Add a new class to your project and paste the code shown below. Compile. Drop the new control from the top of the toolbox onto your form.
using System;
using System.Windows.Forms;
class MyTabControl : TabControl {
public event EventHandler<KeyEventArgs> ArrowKeys;
protected void OnArrowKeys(KeyEventArgs e) {
EventHandler<KeyEventArgs> handler = ArrowKeys;
if (handler != null) handler(this, e);
}
protected override bool ProcessCmdKey(ref Message msg, Keys keyData) {
if (keyData == Keys.Up || keyData == Keys.Down || keyData == Keys.Left || keyData == Keys.Right) {
var e = new KeyEventArgs(keyData);
OnArrowKeys(e);
if (e.Handled) return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
Sample usage in a form:
private void myTabControl1_ArrowKeys(object sender, KeyEventArgs e) {
if (myTabControl1.SelectedIndex == 1) {
// Do something with e.KeyData
//...
e.Handled = true;
}
}
I did this in VB .NET, I can post it in C# if you really need it but this should show you how to handle the catching of the event.
Public Class Form1
'give a variable as a TabPage here so we know which one is selected(in focus)
Dim selectedPage As TabPage = TabPage1
'If a key is pressed when the tab control has focus, it checks to see if it is the right tab page
'and then show a message box(for demonstration)
Private Sub TabControl1_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles TabControl1.KeyDown
'The IF Not is to basically catch any odd happening that might occur if a key stroke gets passed with
'no selected tab page
If Not selectedPage Is Nothing Then
'If the tabpage is TabPage2
If selectedPage.Name = "TabPage2" Then
MessageBox.Show("Key Pressed")
End If
End If
End Sub
'This handles the actual chaning of the tab pages
Private Sub TabControl1_Selected(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TabControlEventArgs) Handles TabControl1.Selected
selectedPage = TabControl1.SelectedTab
End Sub
And now you just need to use the actual key presses.
Michael Sarchet
protected override bool ProcessCmdKey(ref Message m, Keys keyData)
{
bool blnProcess = false;
if (keyData == Keys.Left)
{
blnProcess = true;
MessageBox.Show("Key left");
if (myTabControl1.SelectedIndex == 1)
MessageBox.Show("inside");
}
This code seems to work So when I have selected the tabPage2 a Messagebox tells me "inside" when i press left arrow key.
Probalby not the correct thing to do thing but atleast it works for now...