1

我正在通过 VS 2008 使用托管 C++。我正在创建一个 Windows 窗体应用程序。该应用程序包含 4 个选项卡。用户希望能够简单地按下功能键(在本例中为 F5、F7、F9 或 F10)。. . 将标签页置于最前面。

我知道我必须捕获 KeyDown 事件。这很好用。我知道这一点是因为我在我的 KeyDown 事件处理程序中转储了一些 MessageBox::Show,果然,当按下功能键时,我收到了我的消息。

然而,问题/困境是我似乎无法获得与按下的功能键相对应的 TabPage 以实际成为选定的标签页。我努力了 。. . “BringToFront”、“Focus”、“Enter”和“Click”。这些似乎都无法将 TabPage 放在前面。

这是我的 C++ 代码。. .

System::Void frmBadgeScan_GeneralKeyDown(System::Object^  sender, System::Windows::Forms::KeyEventArgs^  e)
{
    switch (e->KeyCode)
    {
    case System::Windows::Forms::Keys::F3:
        e->Handled = true ;
        if (CurrentTab->Name->Equals("tabEmployeeScanOut"))
            btnClearOutList_Click (sender, nullptr) ;
        else if (CurrentTab->Name->Equals("tabEmployeeScanIn"))
            btnClearInList_Click (sender, nullptr) ;
        break ;

    case System::Windows::Forms::Keys::F5:
        e->Handled = true ;
        MessageBox::Show("F5") ;
        //this->tabEmployeeScanOut->BringToFront () ;
        //this->tabEmployeeScanOut->Focus () ;
        //tabEmployeeScanOut_Enter (sender, nullptr) ;
        break ;

    case System::Windows::Forms::Keys::F9:
        e->Handled = true ;
        MessageBox::Show("F9") ;
        //this->tabEmployeeScanIn->BringToFront () ;
        //this->tabEmployeeScanIn->Focus () ;
        //tabEmployeeScanIn_Enter (sender, nullptr) ;
        break ;
    }
}

System::Void tabEmployeeScanIn_Enter(System::Object^  sender, System::EventArgs^  e)
{
    CurrentTab = this->tabEmployeeScanIn ;
    SendKeys::Send("{Tab}") ;
}

System::Void tabEmployeeScanOut_Enter(System::Object^  sender, System::EventArgs^  e)
{
    CurrentTab = this->tabEmployeeScanOut ;
    SendKeys::Send("{Tab}") ;
}

有任何想法吗?

顺便说一句,变量“CurrentTab”定义为 . . .

    TabPage          ^  CurrentTab ;

在此先感谢您的帮助!

哦,表单上的所有控件都定义为捕获 KeyDown 事件,因此无论哪个控件关注表单,都会触发 KeyDown 事件。. .

像这样。. .

this->stsBadgeScan->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &frmBadgeScan::frmBadgeScan_GeneralKeyDown);

this->tabMainMenu->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &frmBadgeScan::frmBadgeScan_GeneralKeyDown);

this->btnClearOutList->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &frmBadgeScan::frmBadgeScan_GeneralKeyDown);

this->lstScanOut->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &frmBadgeScan::frmBadgeScan_GeneralKeyDown);

this->txtEmplNumScanOut->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &frmBadgeScan::frmBadgeScan_GeneralKeyDown);

this->KeyDown += gcnew System::Windows::Forms::KeyEventHandler(this, &frmBadgeScan::frmBadgeScan_GeneralKeyDown);
4

1 回答 1

1

您必须设置 TabControl 的 SelectedTab 属性。像这样:

this->SomeTabControl->SelectedTab = this->tabEmployeeScanOut;

将“SomeTabControl”替换为选项卡控件的名称,我无法从您的代码中看出。

通过覆盖表单的 ProcessCmdKey() 来避免丑陋的 KeyDown 覆盖。像这样:

protected:
    virtual bool ProcessCmdKey(Message% msg, Keys keyData) override {
        switch (keyData) {
            case Keys::F1: 
                tabControl1->SelectedTab = tabPage1;
                return true;
            case Keys::F2:
                tabControl1->SelectedTab = tabPage2;
                return true;
            // etc...
        }
        return false;
    }
于 2010-12-16T15:01:55.533 回答