TabPages :: 在 KeyDown 上将选项卡置于前面
我正在通过 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);
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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须设置 TabControl 的 SelectedTab 属性。像这样:
将“SomeTabControl”替换为选项卡控件的名称,我无法从您的代码中看出。
通过覆盖表单的 ProcessCmdKey() 来避免丑陋的 KeyDown 覆盖。像这样:
You have to set the SelectedTab property of the TabControl. Like this:
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: