有没有办法禁用 TabControl 内的 TabPage?

发布于 2025-01-01 08:45:37 字数 108 浏览 0 评论 0原文

我真的不需要禁用它们,因为我要么禁用 TabControl 要么启用它。但是当 TabControl 被禁用时,我希望选项卡页面看起来被禁用(灰显)。

I don't really need to disable them because I either disable the TabControl or enable it. But when the TabControl is disabled, I want the tab pages look disabled (greyed out).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

半世蒼涼 2025-01-08 08:45:37

人们下面提到的单独的方法并不能解决问题,但结合起来就可以了。试试这个:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        //Disable tabPage2
        this.tabPage2.Enabled = false; // no casting required.
        this.tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);
        this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
        this.tabControl1.DrawItem += new DrawItemEventHandler(DisableTab_DrawItem);
    }
    private void DisableTab_DrawItem(object sender, DrawItemEventArgs e)
    {
        TabControl tabControl = sender as TabControl;
        TabPage page = tabControl.TabPages[e.Index];
        if (!page.Enabled)
        {
            //Draws disabled tab
            using (SolidBrush brush = new SolidBrush(SystemColors.GrayText))
            {
                e.Graphics.DrawString(page.Text, page.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 3);
            }
        }
        else
        {
            // Draws normal tab
            using (SolidBrush brush = new SolidBrush(page.ForeColor))
            {
                e.Graphics.DrawString(page.Text, page.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 3);
            }
        }
    }

    private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
    {
        //Cancels click on disabled tab.
        if (!e.TabPage.Enabled)
            e.Cancel = true;
    }
}

What people have mentioned below won't do the trick individually, but combined they will. Try this out:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        //Disable tabPage2
        this.tabPage2.Enabled = false; // no casting required.
        this.tabControl1.Selecting += new TabControlCancelEventHandler(tabControl1_Selecting);
        this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
        this.tabControl1.DrawItem += new DrawItemEventHandler(DisableTab_DrawItem);
    }
    private void DisableTab_DrawItem(object sender, DrawItemEventArgs e)
    {
        TabControl tabControl = sender as TabControl;
        TabPage page = tabControl.TabPages[e.Index];
        if (!page.Enabled)
        {
            //Draws disabled tab
            using (SolidBrush brush = new SolidBrush(SystemColors.GrayText))
            {
                e.Graphics.DrawString(page.Text, page.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 3);
            }
        }
        else
        {
            // Draws normal tab
            using (SolidBrush brush = new SolidBrush(page.ForeColor))
            {
                e.Graphics.DrawString(page.Text, page.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 3);
            }
        }
    }

    private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
    {
        //Cancels click on disabled tab.
        if (!e.TabPage.Enabled)
            e.Cancel = true;
    }
}
月下凄凉 2025-01-08 08:45:37

尝试使用此代码,它可以工作:

private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (e.TabPage == tabControl1.TabPages[1])
            {
                e.Cancel = true;
            }            
        }

在 if 条件中保留要禁用的选项卡页的索引或名称,我已将索引保留为 1。
:)

Try using This code it works:

private void tabControl1_Selecting(object sender, TabControlCancelEventArgs e)
        {
            if (e.TabPage == tabControl1.TabPages[1])
            {
                e.Cancel = true;
            }            
        }

Keep the index or name of the tabpage you want to disable in the if condition here i have kept index as 1.
:)

终陌 2025-01-08 08:45:37

重写了 @Corylulu 提供的解决方案,将所有内容封装在控件本身中。

public class DimmableTabControl : TabControl
{
    public DimmableTabControl()
    {
        DrawMode = TabDrawMode.OwnerDrawFixed;
        DrawItem += DimmableTabControl_DrawItem;
        Selecting += DimmableTabControl_Selecting;
    }

    private void DimmableTabControl_DrawItem(object sender, DrawItemEventArgs e)
    {
        TabPage page = TabPages[e.Index];
        using(SolidBrush brush = new SolidBrush(page.Enabled ? page.ForeColor : SystemColors.GrayText))
        {
            e.Graphics.DrawString(page.Text, page.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 3);
        }
    }

    private void DimmableTabControl_Selecting(object sender, TabControlCancelEventArgs e)
    {
        if(!e.TabPage.Enabled)
        {
            e.Cancel = true;
        }
    }
}

Rewrote solution provided by @Corylulu to incapsulate everything in a control itself.

public class DimmableTabControl : TabControl
{
    public DimmableTabControl()
    {
        DrawMode = TabDrawMode.OwnerDrawFixed;
        DrawItem += DimmableTabControl_DrawItem;
        Selecting += DimmableTabControl_Selecting;
    }

    private void DimmableTabControl_DrawItem(object sender, DrawItemEventArgs e)
    {
        TabPage page = TabPages[e.Index];
        using(SolidBrush brush = new SolidBrush(page.Enabled ? page.ForeColor : SystemColors.GrayText))
        {
            e.Graphics.DrawString(page.Text, page.Font, brush, e.Bounds.X + 3, e.Bounds.Y + 3);
        }
    }

    private void DimmableTabControl_Selecting(object sender, TabControlCancelEventArgs e)
    {
        if(!e.TabPage.Enabled)
        {
            e.Cancel = true;
        }
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文