問題描述
我正面臨這樣的問題,我覺得很難克服.在 WinForms 中,我得到了一個(gè)帶有 n 個(gè) TabPages 的 TabControl.我想擴(kuò)展 Ctrl+Tab/Ctrl+Shift+Tab 切換.所以我寫了一些代碼,只要焦點(diǎn)在 TabControl 或 Form 上就可以正常工作.當(dāng)應(yīng)用程序焦點(diǎn)位于 TabPage 內(nèi)部時(shí)(例如,位于 TabPage 內(nèi)部的按鈕上),按下 Ctrl+Tab 時(shí),我的代碼將被忽略,并且 TabControl 會(huì)自行跳到 TabPage(避免我的代碼).
Iam facing such problem, which I find hard to overcome. In WinForms I got a TabControl with n TabPages. I want to extend the Ctrl+Tab / Ctrl+Shift+Tab switching. so I wrote some code which works fine as long as the focus is on TabControl or on the Form. When application focus is INSIDE of TabPage (for example on a button which is placed inside of TabPage), while pressing Ctrl+Tab, my code is ignored and TabControl skips to TabPage on his own (avoiding my code).
有人知道嗎?
推薦答案
您需要從 TabControl 派生并覆蓋 ProcessCmdKey,虛擬方法才能覆蓋 Ctrl-Tab 行為.
You need to derive from TabControl and override ProcessCmdKey, virtual method in order to override the Ctrl-Tab behavior.
例子:
public class ExtendedTabControl: TabControl
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
if (keyData == (Keys.Control | Keys.Tab))
{
// Write custom logic here
return true;
}
if (keyData == (Keys.Control | Keys.Shift | Keys.Tab))
{
// Write custom logic here, for backward switching
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
這篇關(guān)于TabControl 中的 C# Tab 切換的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!