通过单击或TabControl中的键禁用选项卡之间的切换 [英] Disable switching between tabs by click or keys in TabControl

查看:188
本文介绍了通过单击或TabControl中的键禁用选项卡之间的切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



,是否可以仅通过使用NEXT按钮切换到另一个选项卡?



这意味着您不能通过单击另一个选项卡来切换到另一个选项卡页面。 / p>

我通常在NEXT按钮上使用的代码是这样的:

  tabControl1.SelectedTab = tabPage2; 


解决方案

您可以设置 ControlStyles。 UserMouse 设为true。这样,您可以简单地在选项卡标题上禁用鼠标。



顺便说一句,仅禁用标题上的单击是不够的,您需要禁用允许用户切换的键到选项卡之间,例如 Shift + Tab Ctrl + Shift + Tab 首页 End

 使用System.Linq; 
使用System.Windows.Forms;
使用System.ComponentModel;
公共类MyTabControl:TabControl
{
public MyTabControl()
{
if(LicenseManager.UsageMode!= LicenseUsageMode.Designtime)
SetStyle(ControlStyles。 UserMouse,true);
}
受保护的覆盖bool ProcessCmdKey(ref Message msg,Keys keyData)
{
varfilteredKeys = new Keys [] {(Keys.Control | Keys.Tab),
(Keys.Control | Keys.Shift | Keys.Tab),
Keys.Left,Keys.Right,Keys.Home,Keys.End};
if(filteredKeys.Contains(keyData))
返回true;
返回base.ProcessCmdKey(ref msg,keyData);
}
}

注意:想要具有类似向导的控件(不带标题的选项卡控件),则可以像TCM_ADJUSTRECT 3110834>此。您也应该在该解决方案中禁用这些键。这是一个更改的版本:

 使用System.Linq; 
使用System.Windows.Forms;
使用System.ComponentModel;
公共类WizardControl:TabControl
{
受保护的重写bool ProcessCmdKey(ref Message msg,Keys keyData)
{
varfilteredKeys = new Keys [] {(Keys。控制| Keys.Tab),
(Keys.Control | Keys.Shift | Keys.Tab),
Keys.Left,Keys.Right,Keys.Home,Keys.End};
if(filteredKeys.Contains(keyData))
返回true;
返回base.ProcessCmdKey(ref msg,keyData);
}
public const int TCM_FIRST = 0x1300;
public const int TCM_ADJUSTRECT =(TCM_FIRST + 40);
受保护的覆盖无效WndProc(ref Message m)
{
if(m.Msg == TCM_ADJUSTRECT&&!DesignMode)
m.Result =(IntPtr)1;
else
base.WndProc(ref m);
}
}


So guys, is it possible to switch to another tab by ONLY using NEXT button?

This is mean that you CAN'T switch to another tab page by clicking that other tab.

The code that I usually use on the NEXT button are something like this :

tabControl1.SelectedTab = tabPage2;

解决方案

You can set ControlStyles.UserMouse to true. This way you can simply disable mouse on tab headers.

By the way, just disabling click on headers is not enough and you need to disable keys which let the user to switch to between tabs, like Shift+Tab, Ctrl+Shift+Tab, , , Home and End.

using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;
public class MyTabControl : TabControl
{
    public MyTabControl()
    {
        if (LicenseManager.UsageMode != LicenseUsageMode.Designtime)
            SetStyle(ControlStyles.UserMouse, true);
    }
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        var filteredKeys = new Keys[]{(Keys.Control | Keys.Tab),
            (Keys.Control | Keys.Shift | Keys.Tab),
            Keys.Left, Keys.Right, Keys.Home, Keys.End};
        if (filteredKeys.Contains(keyData))
            return true;
        return base.ProcessCmdKey(ref msg, keyData);
    }
}

Note: If you like to have a wizard-like control (tab control without header), you can handle TCM_ADJUSTRECT like this. You should disable those keys also in that solution too. Here is a changed version:

using System.Linq;
using System.Windows.Forms;
using System.ComponentModel;
public class WizardControl: TabControl
{
    protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
    {
        var filteredKeys = new Keys[]{(Keys.Control | Keys.Tab),
            (Keys.Control | Keys.Shift | Keys.Tab),
            Keys.Left, Keys.Right, Keys.Home, Keys.End};
        if (filteredKeys.Contains(keyData))
            return true;
        return base.ProcessCmdKey(ref msg, keyData);
    }
    public const int TCM_FIRST = 0x1300;
    public const int TCM_ADJUSTRECT = (TCM_FIRST + 40);
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == TCM_ADJUSTRECT && !DesignMode)
           m.Result = (IntPtr)1;
        else 
           base.WndProc(ref m);
    }
}

这篇关于通过单击或TabControl中的键禁用选项卡之间的切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆