gpt4 book ai didi

c# - 有没有办法在 TabControl 中禁用 TabPage?

转载 作者:行者123 更新时间:2023-11-30 13:33:13 24 4
gpt4 key购买 nike

我真的不需要禁用它们,因为我要么禁用 TabControl 要么启用它。但是当 TabControl 被禁用时,我希望标签页看起来被禁用(变灰)。

最佳答案

人们在下面提到的单独使用不会成功,但结合起来就可以了。试试这个:

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

关于c# - 有没有办法在 TabControl 中禁用 TabPage?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9087807/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com