Results 1 to 7 of 7

Thread: [RESOLVED] Tab control on a custom control

  1. #1

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    32

    Resolved [RESOLVED] Tab control on a custom control

    Hi, I am working on a user control. In my control, there is a tab control with three tab pages. When the user control is compiled and placed in a windows forms project, tab control does not function at all. My main purpose is to be able to add some buttons on different pages of tab control, but I even can’t change the active tab page because it is not working. Can anyone help me about this problem please? Thank you.

  2. #2
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Tab control on a custom control

    The problem is that the TabControl is not 'design-time activated' because it is on a UserControl.

    You can activate it by creating a custom ControlDesigner for your UserControl and using its EnableDesignMode method to enable the design-time of the TabControl.

    An important remark on that MSDN page is this:
    The child control specified by child is a child of this control designer's control. The child does not directly participate in persistence, but it will if it is exposed as a property of the main control. Consider a control like the SplitContainer: it has two panels, Panel1 and Panel2. These panels are exposed through read only Panel1 and Panel2 properties on the SplitContainer control. The SplitContainer control's designer calls EnableDesignMode for each panel, which allows other components to be dropped on them. But, in order for the contents of Panel1 and Panel2 to be saved, the SplitContainer control itself must expose the panels as public properties.
    It basically says you must expose your TabControl via a Property as well, otherwise any controls dropped on it during design-time will not persist to run-time and you will lose them. More importantly, you will need to add the DesignerSerializationVisibility attribute (and set it to Content) so that the properties of the Content of the tabcontrol is saved (and not just the properties of the TabControl).


    To summarize: this is how you create and apply a custom ControlDesigner. First, you'll have to add a reference to System.Design. For this to work, if you are using VS2010, you will have to set your target framework to .NET 2.0, 3.5 or 4.0, but not any of the 'Client Profile' frameworks, as they don't contain this assembly. Just rightclick the project, select Add Reference and browse to System.Design in the .NET tab.

    Now, if your UserControl is called MyControl, you can give it a custom ControlDesigner by using something like this:
    vb.net Code:
    1. Imports System.Windows.Forms.Design
    2. Imports System.ComponentModel
    3.  
    4. <Designer(GetType(MyControl.MyControlDesigner))> _
    5. Public Class MyControl
    6.  
    7.     Friend Class MyControlDesigner
    8.         Inherits ControlDesigner
    9.  
    10.         '  TODO: Add/Override methods to change the design-time behavior
    11.     End Class
    12.  
    13. End Class

    As I said, first you have to add a property to MyControl that exposes your TabControl and uses the DesignerSerializationVisibility attribute:
    vb.net Code:
    1. <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    2.     Public ReadOnly Property TabControl As TabControl
    3.         Get
    4.             Return Me.TabControl1   'assuming TabControl1 is the name of your TabControl
    5.         End Get
    6.     End Property

    Finally, you'll have to override the Initialize method of the MyControlDesigner class to call the EnableDesignMode method. For convenience, you can also add a property that returns the control the designer is currently designing as type 'MyControl' (instead of type 'Control' which is pretty useless to you, since you need the TabControl property):
    vb.net Code:
    1. Friend Class MyControlDesigner
    2.         Inherits ControlDesigner
    3.  
    4.         Public ReadOnly Property MyControl As MyControl
    5.             Get
    6.                 Return DirectCast(Me.Control, MyControl)
    7.             End Get
    8.         End Property
    9.  
    10.         Public Overrides Sub Initialize(ByVal component As System.ComponentModel.IComponent)
    11.             MyBase.Initialize(component)
    12.  
    13.             If Me.MyControl IsNot Nothing Then
    14.                 Me.EnableDesignMode(Me.MyControl.TabControl, "TabControl1")
    15.             End If
    16.         End Sub
    17.     End Class

    Let's see if you can put it all together.

  3. #3

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    32

    Re: Tab control on a custom control

    NickThissen, thank you so much for replying my question. I really appreciate your answer. I did exactly what you said in your post and compiled the dll successfully. When I add the control to a window forms project, tab responds me. I can move from one tab page to another. However when I drag and drop a control onto the tabcontrol I get the following error message:

    The control System.Windows.Forms.TabControl has thrown an unhandled exception in the designer and has been disabled.

    Exception:
    Object referance not set to an instance of an object.

    Stack trace:

    I also added the error message as attachment.

    My final code looks like this:

    Imports System.Windows.Forms.Design
    Imports System.ComponentModel

    <Designer(GetType(MyControl.MyControlDesigner))> _
    Public Class MyControl
    <DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
    Public ReadOnly Property TabControl() As TabControl
    Get
    Return Me.TabControl1 'assuming TabControl1 is the name of your TabControl
    End Get
    End Property

    Friend Class MyControlDesigner
    Inherits ControlDesigner


    Public ReadOnly Property MyControl() As MyControl
    Get
    Return DirectCast(Me.Control, MyControl)
    End Get
    End Property

    Public Overrides Sub Initialize(ByVal component As System.ComponentModel.IComponent)
    MyBase.Initialize(component)

    If Me.MyControl IsNot Nothing Then

    Me.EnableDesignMode(Me.MyControl.TabControl, "TabControl1")


    End If
    End Sub

    End Class

    End Class
    Attached Images Attached Images  
    Last edited by aliefeozkan; Sep 7th, 2010 at 05:31 AM.

  4. #4
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Tab control on a custom control

    That's strange, it works just fine for me except for the fact that any TabPages you added in the UserControl designer aren't available in the Form designer. Besides that, I can simply add a new tab and add any control to that tab. Even if I try to add a control to the 'disabled' tabpages (the two default ones) I don't get any errors.

    What version of VS are you using? Can you get this error consistently or was it a one-time thing? Can you zip up your project and attach it so I can take a look and see if I can get the same behavior?

  5. #5

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    32

    Re: Tab control on a custom control

    I am using VS.2008, and unfortunately I receive the error consistently. I attached project files in a zip file as you said. If you could check it I’ll be so glad. Thanks again for your help.
    Attached Files Attached Files

  6. #6
    PowerPoster
    Join Date
    Apr 2007
    Location
    The Netherlands
    Posts
    5,070

    Re: Tab control on a custom control

    I don't get the error in VS2010. Perhaps they fixed something, but I am sure that I've used this EnableDesignMode method in VS2008 as well without problems. That's a bit strange.

    What happens if you add the project to a new winforms project? Do you get the same error? If not it might be getting 'corrupted' (in whatever way) by your other projects. Try removing the bin and obj folders from the project(s) and recompiling everything, that can sometimes help.

    Anyway, another thing I saw is that you are now also manually enabling the first Tabpage for design-mode. I looked at your code first and thought that was surely the issue, but apparently it's not... It might still be causing trouble in VS2008 though. Does it throw the same error if you don't enable the TabPage? I think it's much better to remove all the TabPages in the UserControl designer (just open MyControl design and remove them, then rebuild) and simply let the user (you?) add the TabPages back when using MyControl (so, in the form designer). The TabControl is fully design-mode enabled which means you can access the TabPages property and add a Tabpage, or even simply use the 'Action List' or 'Smart Tags' (the [>] button on the top-right corner) to add the tabpages.
    For some reason any existing TabPages are not design-mode enabled (which I'm sure is why you are enabling them manually), but TabPages you add later are enabled without any problems (at least for me...).

  7. #7

    Thread Starter
    Member
    Join Date
    Feb 2010
    Posts
    32

    Re: Tab control on a custom control

    After all my tries, I could not manage to make it work. But after reading your post, I decided to use couple of panels instead of a tab control. And it worked! Thank you so much for your helpful approach and cooperation.

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