Hi,
I have a TabControl on a UserControl. I want the UserControl to behave like a TabControl as usual (it contains only the TabControl and a Contextmenu), including Design-time behavior.
I have spent the last two weeks finding out how to add the Design-time behavior and I think I have come a far way.
I finally found a concrete question I can ask you, since I am really stuck on this.
The problem is with adding TabPages during Design-time (while the UserControl is a custom tabcontrol, it is using regular windows forms TabPages).
In the Designer class, I have the following code to add a TabPage:
(tc is an instance of my custom TabControl)vb.net Code:
Dim dh As IDesignerHost = DirectCast(GetService(GetType(IDesignerHost)), IDesignerHost) If dh IsNot Nothing Then Dim i As Integer = tc.SelectedIndex Dim name As String = GetNewTabName() Dim tab As TabPage = TryCast(dh.CreateComponent(GetType(TabPage), name), TabPage) tab.Text = name tc.Controls.Add(tab) tc.SelectedTab = tab RaiseComponentChanging(TypeDescriptor.GetProperties(Control)("SelectedIndex")) RaiseComponentChanged(TypeDescriptor.GetProperties(Control)("SelectedIndex"), i, tc.SelectedIndex) End If
I got this code from another (C#.NET) custom TabControl. The problem here is that with the other C#.NET tabcontrol, the usercontrol IS the tabcontrol.
With me, the usercontrol is a regular usercontrol that CONTAINS a tabcontrol.
The difference might seem small but it is a huge difference when trying to add TabPages (at least I think so)!
As you can see, the code above adds a TabPage by using:
In the C# project this works because tc.Controls would mean the tabcontrol collection.Code:tc.Controls.Add(tab)
In my project, tc.Controls returns the windows forms TabControl and the Contextmenu. NOT the TabPages!
So, I thought, that would be an easy fix: simply change "tc.Controls" to "tc.tabCtrl.TabPages" (where 'tabCtrl' is the windows forms TabControl).
However, there is one problem: If I do this in the designer, the tabs DO NOT GET ADDED to my custom TabControl!! I can see this easily in the Form Designer file.
With a regular TabControl (TabControl1) after adding two tabpages I get:
Note the "Me.TabControl1.Controls.Add" part: this is where the TabPages are added to the TabControl.vb.net Code:
' 'TabControl1 ' Me.TabControl1.Controls.Add(Me.TabPage3) Me.TabControl1.Controls.Add(Me.TabPage4) Me.TabControl1.Location = New System.Drawing.Point(250, 192) Me.TabControl1.Name = "TabControl1" Me.TabControl1.SelectedIndex = 0 Me.TabControl1.Size = New System.Drawing.Size(200, 100) Me.TabControl1.TabIndex = 1
Now my custom tabcontrol (CTabControl1):
As you can see, no such thing! The tabpages are not added.vb.net Code:
' 'CTabControl1 ' Me.CTabControl1.Location = New System.Drawing.Point(12, 12) Me.CTabControl1.Name = "CTabControl1" Me.CTabControl1.SelectedIndex = 1 Me.CTabControl1.SelectedTab = Me.TabPage2 Me.CTabControl1.Size = New System.Drawing.Size(200, 186) Me.CTabControl1.TabIndex = 0
So: I think I HAVE to use 'tc.Controls.Add' but I CAN'T because controls does not refer to the TabPages collection!!
How can I solve this?
I have tried shadowing the Controls collection and returning the TabPages instead of the Controls collection, but then I obviously got errors when the designer tries to add the windows forms TabControl + contexmenu...
I am really clueless... Is this even possible??
Thanks!
EDIT
I had a new idea and was confident that it would work but no...
I tried overriding the 'OnControlAdded' method by doing this:
So it would add the control to the TabPages collection if it was a TabPage, and use the regular routine otherwise.vb.net Code:
Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs) If TypeOf e.Control Is TabPage Then Me.tabCtrl.Controls.Add(CType(e.Control, TabPage)) Else MyBase.OnControlAdded(e) End If End Sub
(I'm now using 'tc.Controls.Add(tab)' again instead of 'tc.tabCtrl.TabPages.Add')
But, I don't think the designer even gets that far because it is telling me I cannot add TabPages to my usercontrol, because only TabControls can accept TabPages... How can I make my usercontrol understand that it is a TabControl (without Inheriting from a TabControl?)



Reply With Quote