开发者

C# how to trigg a key event in a tabcontrol specific tab?

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;
  }
}


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


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


private void Main_KeyUp(object sender, KeyEventArgs e)
{
    if (e.KeyCode == Keys.C) // I used the C Key
    {
        if (tabControl.SelectedIndex == 0) // control is only in tab 1 (index 0) endabled
        {
            // Your Code
        }
    }
}


A C# version of msarchet's answer:

using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;

public class Form1
{

    // give a variable as a TabPage here so we know which one is selected(in focus)
    private TabPage selectedPage = 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 void TabControl1_KeyDown(System.Object sender, System.Windows.Forms.KeyEventArgs e)
    {
        // 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 (!selectedPage == null)
        {
            // If the tabpage is TabPage2
            if (selectedPage.Name == "TabPage2")
                MessageBox.Show("Key Pressed");
        }
    }

    // This handles the actual chaning of the tab pages
    private void TabControl1_Selected(System.Object sender, System.Windows.Forms.TabControlEventArgs e)
    {
        selectedPage = TabControl1.SelectedTab;
    }
}

I hope this helps you :)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜