Results 1 to 4 of 4

Thread: [RESOLVED] [2008] TabControl on UserControl - Adding Tabs (Design-time)

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Resolved [RESOLVED] [2008] TabControl on UserControl - Adding Tabs (Design-time)

    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:
    vb.net Code:
    1. Dim dh As IDesignerHost = DirectCast(GetService(GetType(IDesignerHost)), IDesignerHost)
    2.  
    3.         If dh IsNot Nothing Then
    4.             Dim i As Integer = tc.SelectedIndex
    5.             Dim name As String = GetNewTabName()
    6.             Dim tab As TabPage = TryCast(dh.CreateComponent(GetType(TabPage), name), TabPage)
    7.             tab.Text = name
    8.             tc.Controls.Add(tab)
    9.             tc.SelectedTab = tab
    10.             RaiseComponentChanging(TypeDescriptor.GetProperties(Control)("SelectedIndex"))
    11.             RaiseComponentChanged(TypeDescriptor.GetProperties(Control)("SelectedIndex"), i, tc.SelectedIndex)
    12.         End If
    (tc is an instance of my custom TabControl)

    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:
    Code:
    tc.Controls.Add(tab)
    In the C# project this works because tc.Controls would mean the tabcontrol collection.

    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:
    vb.net Code:
    1. '
    2.         'TabControl1
    3.         '
    4.         Me.TabControl1.Controls.Add(Me.TabPage3)
    5.         Me.TabControl1.Controls.Add(Me.TabPage4)
    6.         Me.TabControl1.Location = New System.Drawing.Point(250, 192)
    7.         Me.TabControl1.Name = "TabControl1"
    8.         Me.TabControl1.SelectedIndex = 0
    9.         Me.TabControl1.Size = New System.Drawing.Size(200, 100)
    10.         Me.TabControl1.TabIndex = 1
    Note the "Me.TabControl1.Controls.Add" part: this is where the TabPages are added to the TabControl.

    Now my custom tabcontrol (CTabControl1):
    vb.net Code:
    1. '
    2.         'CTabControl1
    3.         '
    4.         Me.CTabControl1.Location = New System.Drawing.Point(12, 12)
    5.         Me.CTabControl1.Name = "CTabControl1"
    6.         Me.CTabControl1.SelectedIndex = 1
    7.         Me.CTabControl1.SelectedTab = Me.TabPage2
    8.         Me.CTabControl1.Size = New System.Drawing.Size(200, 186)
    9.         Me.CTabControl1.TabIndex = 0
    As you can see, no such thing! The tabpages are not added.


    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:
    vb.net Code:
    1. Protected Overrides Sub OnControlAdded(ByVal e As System.Windows.Forms.ControlEventArgs)
    2.         If TypeOf e.Control Is TabPage Then
    3.             Me.tabCtrl.Controls.Add(CType(e.Control, TabPage))
    4.         Else
    5.             MyBase.OnControlAdded(e)
    6.         End If
    7.     End Sub
    So it would add the control to the TabPages collection if it was a TabPage, and use the regular routine otherwise.

    (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?)

  2. #2

  3. #3
    New Member
    Join Date
    Mar 2008
    Posts
    2

    Re: [RESOLVED] [2008] TabControl on UserControl - Adding Tabs (Design-time)

    Could you tell us hot did you make this possible. i really need this.

    Thank in advance.

  4. #4

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width