Rewrote solution provided by @corylulu and @antgraf to add tracking of enabled changed events from the TabPages to redraw the TabControl:
public class DimmableTabControl : TabControl
{
public DimmableTabControl() {
DrawMode = TabDrawMode.OwnerDrawFixed;
DrawItem += DimmableTabControl_DrawItem;
Selecting += DimmableTabControl_Selecting;
ControlAdded += DimmableTabControl_ControlAdded;
ControlRemoved += DimmableTabControl_ControlRemoved;
}
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;
}
}
private void DimmableTabControl_ControlAdded(object sender, ControlEventArgs e) {
e.Control.EnabledChanged += DimmableTab_EnabledChanged;
}
private void DimmableTabControl_ControlRemoved(object sender, ControlEventArgs e) {
e.Control.EnabledChanged -= DimmableTab_EnabledChanged;
}
private void DimmableTab_EnabledChanged(object sender, EventArgs e) {
Control control = (Control) sender;
control.Parent?.Refresh();
}
}