我的表单中有一个TabControl1,其中有三个分别名为TabPage1,TabPage2和TabPage3的TabPage。
当TabPage 2具有焦点时,我需要引发一个键事件(用于导航的箭头键)。此事件不应在其他TabPage中引发。
任何人都知道吗?
在选定的事件处理程序上,您可以将发送方转换为适当的控件,并检查其名称。如果事件是从TabPage2生成的,则可以触发关键事件。
这样的东西
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);
}
}
样本使用情况:
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");
}
}
此代码似乎有效因此,当我选择tabPage2时,当我按向左箭头键时,消息框会告诉我“内部”。
Probalby做某事不是正确的事,但至少现在可以起作用...
我在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
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
}
}
}
msarchet的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;
}
}
希望对您有帮助:)