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

C#如何在tabcontrol特定选项卡中触发关键事件?,c#,events,key,tabcontrol,C#,Events,Key,Tabcontrol,我的表单中有一个tabControl1,它有三个TabPage1、TabPage2和TabPage3 当TabPage 2有焦点时,我需要引发一个键事件(用于导航的箭头键)。 不应在其他选项卡页中引发此事件 有人知道怎么做吗?在选定的事件处理程序上,您可以将发送者强制转换到适当的控件并检查其名称。如果事件是从TabPage2生成的,则可以触发key事件 像这样的 private void TabPage_Selected(object sender, EventArgs e) { TabPa

我的表单中有一个tabControl1,它有三个TabPage1、TabPage2和TabPage3

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


有人知道怎么做吗?

在选定的事件处理程序上,您可以将发送者强制转换到适当的控件并检查其名称。如果事件是从TabPage2生成的,则可以触发key事件

像这样的

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

在选定的事件处理程序上,您可以将发送方强制转换为适当的控件,并检查其名称。如果事件是从TabPage2生成的,则可以触发key事件

像这样的

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

您需要从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);
  }
}

您需要从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);
  }
}

我是在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
现在你只需要使用实际的按键


Michael Sarchet

我是在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
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");  


             }
       }
现在你只需要使用实际的按键

迈克尔·萨切特

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时,当我按下向左箭头键时,一个消息框会告诉我“内部”

也许做这件事不是正确的,但至少它现在起作用了

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
        }
    }
}
这个代码似乎有效 因此,当我选择tabPage2时,当我按下向左箭头键时,一个消息框会告诉我“内部”

也许做这件事并不正确,但至少目前它是有效的…

C版本的答案:

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
        }
    }
}
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;
    }
}
我希望这对您有所帮助:)

的C版本的答案:

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


我希望这能帮助你:)

老实说,从设计的角度来看,你想要的东西对我来说有点奇怪。如果你描述一下你真正的任务也许会更好?你的问题很不清楚。当你说“加薪”时,你的意思是“处理”吗?对不起,不清楚。当TabPage2有焦点时,我需要做些事情。例如,当我按向上箭头键时,它将更改TabPage2中的一些标签。但这只会出现在TabPage2中,而不会出现在其他tabpages中。老实说,从设计的角度来看,你想要的东西对我来说有点奇怪。如果你描述一下你真正的任务也许会更好?你的问题很不清楚。当你说“加薪”时,你的意思是“处理”吗?对不起,不清楚。当TabPage2有焦点时,我需要做些事情。例如,当我按向上箭头键时,它将更改TabPage2中的一些标签。但是这只会在TabPage2中出现,而不会在其他tabpages中出现。private void tabControl1_KeyDown(对象发送者,KeyEventArgs e){MessageBox.Show(“按键按下”);}当按下一个键并且这个tabControl1有焦点时,是否应该给我一个MessageBox?Mamoo:由于某些原因,您的代码无法工作可能事件处理程序名称和/或禁忌页面控件名称不相同。不管怎么说,在这番评论之后,事情有点不明朗。。。预期的行为是:如果按下某个键并且焦点在某个选项卡页上,则显示消息(或执行其他操作…)?我认为问题在于选项卡页不能有焦点(页面上没有可选择的对象,只有一堆标签)。我们必须检查是否选择了正确的制表符,然后在按键时执行操作Private void tabControl1_KeyDown(对象发送者,KeyEventArgs e){MessageBox.Show(“按键按下”);}当按键时以及当此TabControls具有焦点时,是否应该给我一个MessageBox?Mamoo:由于某些原因,您的代码无法工作可能事件处理程序名称和/或禁忌页面控件名称不相同。不管怎么说,在这番评论之后,事情有点不明朗。。。预期的行为是:如果按下某个键并且焦点在某个选项卡页上,则显示消息(或执行其他操作…)?我认为问题在于选项卡页不能有焦点(页面上没有可选择的对象,只有一堆标签)。我们必须检查是否选择了正确的选项卡,然后在按键时进行操作。我无法管理它工作。当按下左/右箭头时,它只是在标签之间切换。焦点在哪里?它不在选项卡页内的控件上吗?选项卡页只有标签。我想这就是问题所在。我想用F2+F3+F4键导航到不同的标签,然后当选择了Tab Page2时,我需要注意箭头键,因为不同的标签都是数字,应该改变颜色以查看哪个标签是“选中的”(没有选择,只有视觉上)。标签是一组数字,我需要使用pageup和pagedown键更改单个标签(数字)。当标签页只包含标签时,发布的代码可以正常工作。你真的把ArrowKeys事件联系起来了吗?只是粘贴代码不起作用。在“属性”窗口中,单击闪电图标,然后双击箭头键。我无法使其工作。当按下左/右箭头时,它只是在标签之间切换。焦点在哪里?它不在选项卡页内的控件上吗?选项卡页只有标签。我想这就是问题所在。我想用F2+F3+F4键导航到不同的标签,然后当选择了Tab Page2时,我需要注意箭头键,因为不同的标签都是数字,应该改变颜色以查看哪个标签是“选中的”(没有选择,只有视觉上)。标签是一堆数字,我需要更改