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.
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:
Return Me.TabControl1 'assuming TabControl1 is the name of your TabControl
End Get
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:
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)
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.
<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)
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?
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.
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...).
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.