Reputation: 12184
When new TabControl is placed in designer, it comes with two default TabPage pages:
I can easily inherit and modify TabControl itself, but
how can I intercept creation of tab pages and set their properties?
UseVisualStyleBackColor = false
by default for every TabPage.(C# or VB – whatever you prefer.)
Upvotes: 2
Views: 802
Reputation: 125197
Inherit TabControl and Override OnControlAdded method.
class MyTabControl : TabControl
{
protected override void OnControlAdded(ControlEventArgs e)
{
base.OnControlAdded(e);
var page = e.Control as TabPage;
if (page != null)
{
page.UseVisualStyleBackColor = false;
page.BackColor = Color.Red;
}
}
}
This way, if you add TabPage using code or using designer, your settings will applied.
In this case, Inheritance works better that event handling, because there is no need to handle ControlAdded event on every form you have in project.
Upvotes: 1
Reputation: 12184
For convenience of others, I'm sharing what I finally implemented.
Credits go to @joehanna for idea and @Reza Aghaei for clean code. So my solution is based on their contributions:
Public Class TabBasedMultipage : Inherits TabControl
Protected Overrides Sub OnControlAdded(e As ControlEventArgs)
MyBase.OnControlAdded(e)
Dim tabPage As TabPage = TryCast(e.Control, TabPage)
If tabPage IsNot Nothing Then
tabPage.UseVisualStyleBackColor = False
End If
End Sub
End Class
Upvotes: 1
Reputation: 1489
You could handle the ControlAdded
event and test the Control that was added and work on it accordingly:
Private Sub TabControl1_ControlAdded(sender As Object, e As ControlEventArgs) Handles TabControl1.ControlAdded
Debug.WriteLine("Something added: " & e.Control.Name & " " & e.Control.GetType().ToString)
If TypeOf e.Control Is TabPage Then
Dim tp As TabPage = CType(e.Control, TabPage)
tp.UseVisualStyleBackColor = False
End If
End Sub
Upvotes: 4