我的表单中有一个tabControl1,其中有三个名为TabPage1、TabPage2和TabPage3的TabPages。
当TabPage 2具有焦点时,我需要引发一个按键事件(用于导航的箭头键)。此事件不应在其他TabPages中引发。
有人知道怎么做吗?
发布于 2020-02-16 16:43:24
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;
    }
}我希望这对您有帮助:)
https://stackoverflow.com/questions/2128711
复制相似问题