C# 如何触发 tabcontrol 特定选项卡中的按键事件?

发布于 2024-08-18 14:50:04 字数 160 浏览 8 评论 0原文

我的表单中有一个 tabControl1,其中包含三个名为 TabPage1、TabPage2 和 TabPage3 的 TabPage。

当 TabPage 2 获得焦点时,我需要引发一个按键事件(用于导航的箭头键)。 不应在其他选项卡页中引发此事件。

有人知道怎么做吗?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

镜花水月 2024-08-25 14:50:04

在选定的事件处理程序上,您可以将发送者强制转换为正确的控件并检查其名称。如果事件是从 TabPage2 生成的,您可以触发按键事件。

像这样的东西

private void TabPage_Selected(object sender, EventArgs e)
{
  TabPage source = sender as TabPage;
  if(source.Name.equals("TabPage2"))
    //Do whatever...
}

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...
}
标点 2024-08-25 14:50:04

您需要从 TabControl 派生自己的控件,以便可以拦截箭头键并生成事件。将新类添加到您的项目中并粘贴下面所示的代码。编译。将新控件从工具箱顶部拖放到窗体上。

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

表单中的示例用法:

private void myTabControl1_ArrowKeys(object sender, KeyEventArgs e) {
  if (myTabControl1.SelectedIndex == 1) {
    // Do something with e.KeyData
    //...
    e.Handled = true;
  }
}

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;
  }
}
怪我入戏太深 2024-08-25 14:50:04
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");  


             }
       }

这段代码似乎有效
因此,当我选择 tabPage2 时,当我按向左箭头键时,消息框会告诉我“内部”。

可能不是正确的做法,但至少现在有效......

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

满地尘埃落定 2024-08-25 14:50:04

我在 VB .NET 中完成了此操作,如果您确实需要,我可以将其发布在 C# 中,但这应该向您展示如何处理事件的捕获。

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

现在您只需要使用实际的按键即可。

迈克尔·萨切特

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

深陷 2024-08-25 14:50:04
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
        }
    }
}
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
        }
    }
}
不弃不离 2024-08-25 14:50:04

msarchet 的答案的 AC# 版本:

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

我希望这对您有帮助:)

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 :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文