开发者

TabPages :: Bring Tab To Front on KeyDown

I am working in Managed C++ via VS 2008. I am creating a Windows form app. The application contains 4 tabs. The user wants to be able to simply press a function key (in this case, F5, F7, F9 or F10) . . . to bring a tab page to the front.

I know I have to capture the KeyDown event. That works fine. I know this because I dumped some MessageBox::Show's in my KeyDown event handler and sure enough, I am getting my messages back when the Function keys are pressed.

The problem / dilemma however is that I can't seem to get the TabPage that corresponds to the Function Key Pressed to actually become the selected Tab Page. I have tried . . . "BringToFront", "Focus", "Enter" and "Click". None of these seem to do the trick to bringing the TabPage to the front.

Here's my C++ Code . . .

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

Any ideas?

BTW, the variable "CurrentTab" is defined as . . .

    TabPage          ^  CurrentTab ;

Thanks in advance for the help!

Oh, and all of the controls on the 开发者_开发百科form are defined to capture the KeyDown event so regardless of what control has focus on the form, the KeyDown event will be fired . . .

Like so . . .

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


You have to set the SelectedTab property of the TabControl. Like this:

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

Replace "SomeTabControl" with the name of the tab control, I can't tell from your code.

Avoid the ugly KeyDown overrides by overriding the form's ProcessCmdKey(). Like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜