I have a tabcontrol on a form. If I add tab pages and add controls to them in Visual Studio the controls become members of the main form and the main form will also handle all events from all controls on all tab pages.I want to create my own TabPage class that handles everything that happens in the TabPage to avoid having all code in the main form.
I created MyTabPage class by adding a new Component to the project and changing the base class to TabPage. I am able to drag stuff from the Toolbox onto my TabPage in Design mode.
In the designer for the main form I added another tab page to the tab control and changed the type from TabPage to MyTabpage in the main form code.
However, I am not able to see any controls in the tab when I launch the application. If I set a background image it is shown, though. How am I supposed to handle the controls to make them visible? There seems to be no design possibilitiesforMyTabPage other than dragging stuff from the toolbox.
| | malj Friday, August 18, 2006 8:35 AM | Add the ToolBoxItem Attribute to your Custom TabPage Class and you will be able to drag instances of it from the Toolbox.
<System.ComponentModel.ToolboxItem(True)>_ Public Class MyTabPage Inherits TabPage
You could also create a custom TabControl to which you can add custom TabPages via a Custom CollectionEditor.
A simple example in VB.net follows (note that you will need to add a reference to System.Design.dll) Imports System.ComponentModel
Imports System.drawing.Design
Public Class MyTabControl
Inherits System.Windows.Forms.TabControl
<Editor(GetType(TabPageCollectionEditor), GetType(UITypeEditor))> _
Public Shadows ReadOnly Property TabPages() As TabPageCollection
Get
Return MyBase.TabPages
End Get
End Property
End Class
Public Class RandomColorTabPage
Inherits System.Windows.Forms.TabPage
Public Sub New()
MyBase.New()
Me.BackColor = RandomColor()
End Sub
Public Function RandomColor() As Color
Static rand As New Random
Return Color.FromArgb(255, rand.Next(256), rand.Next(256), rand.Next(256))
End Function
End Class
Public Class TabPageCollectionEditor
Inherits System.ComponentModel.Design.CollectionEditor
Public Sub New(ByVal type As Type)
MyBase.New(type)
End Sub
Protected Overrides Function CreateCollectionItemType() As System.Type
Return GetType(TabPage)
End Function
Protected Overrides Function CreateNewItemTypes() As System.Type()
Return New Type() {GetType(TabPage), GetType(RandomColorTabPage)}
End Function
End Class
There is a similar example on my site which includes a custom Designer, and for which code is available in both VB.net and C#.
http://www.dotnetrix.co.uk/tabcontrols.html
| | Mick Doherty Friday, August 18, 2006 11:19 AM | Add the ToolBoxItem Attribute to your Custom TabPage Class and you will be able to drag instances of it from the Toolbox.
<System.ComponentModel.ToolboxItem(True)>_ Public Class MyTabPage Inherits TabPage
You could also create a custom TabControl to which you can add custom TabPages via a Custom CollectionEditor.
A simple example in VB.net follows (note that you will need to add a reference to System.Design.dll) Imports System.ComponentModel
Imports System.drawing.Design
Public Class MyTabControl
Inherits System.Windows.Forms.TabControl
<Editor(GetType(TabPageCollectionEditor), GetType(UITypeEditor))> _
Public Shadows ReadOnly Property TabPages() As TabPageCollection
Get
Return MyBase.TabPages
End Get
End Property
End Class
Public Class RandomColorTabPage
Inherits System.Windows.Forms.TabPage
Public Sub New()
MyBase.New()
Me.BackColor = RandomColor()
End Sub
Public Function RandomColor() As Color
Static rand As New Random
Return Color.FromArgb(255, rand.Next(256), rand.Next(256), rand.Next(256))
End Function
End Class
Public Class TabPageCollectionEditor
Inherits System.ComponentModel.Design.CollectionEditor
Public Sub New(ByVal type As Type)
MyBase.New(type)
End Sub
Protected Overrides Function CreateCollectionItemType() As System.Type
Return GetType(TabPage)
End Function
Protected Overrides Function CreateNewItemTypes() As System.Type()
Return New Type() {GetType(TabPage), GetType(RandomColorTabPage)}
End Function
End Class
There is a similar example on my site which includes a custom Designer, and for which code is available in both VB.net and C#.
http://www.dotnetrix.co.uk/tabcontrols.html
| | Mick Doherty Friday, August 18, 2006 11:19 AM |
Added the attribute like [System.ComponentModel. ToolboxItem(true)] since I am using C#. However, I am not able to drag the class from the solution explorer tothe toolbox. Is that the way I am suppoed to do it? I get an item like "Text: C:\Program Files\....\MyTabPage.cs".
Anyway, myproblem is thatthe controls on mytab pageare not visible when I use it in a tab control. Will that work if I drag it from the toolbox? WiIl I then be able to design my tab page (not just adding toolbox items to it)? | | malj Monday, August 21, 2006 7:03 AM | Right Click on the Toolbox and select "Add/Remove..." or "Choose Items..." (depends on version of VS)
Browse to the Bin directory of your App and select the exe/dll file
The Custom TabPage will appear as a control on the toolbox.
However, it sounds very much like you want to Design your TabPages seperately from the Form. This is not the solution for that.
UserControls are the only controls which are Designable. This statement may not be true for VS2005, but I'm not using that and so can't say whether this is the case in that environment.
So your options are:
- Create UserControls. Then add them to a TabPage in a TabControl (Dock to Fill).
- Create UserControls and once Finished, close the Designer and Change Inheritance to TabPage.
Option 2 does work, but you must make sure that you never open the Designer whilst the Class Inherits from TabPage, otherwise the layout is destroyed. If you need to make design changes to the class, then make sure you temporarily change Inheritance back to UserControl.
You could of course always create your own Designer, but I suspect that this is a lot more work than you want to do. | | Mick Doherty Monday, August 21, 2006 3:14 PM | Malj,
Did you ever get this to work? I am having the exact same problem.
| | Rick Kirch Tuesday, September 25, 2007 6:09 PM | use a UserControl as outlined in Mick Doherty's post
- Proposed As Answer byrickfoster Friday, July 03, 2009 6:01 PM
-
| | RightPaddock Thursday, September 27, 2007 4:31 AM | Actually I found out my specific problem, and I believe it is a bug with the IDE. I followed the directions of a Microsoft MSDN recommendation (http://msdn2.microsoft.com/en-us/library/7h62478z(VS.80).aspx) on how to inherit from existing Windows Forms Controls.I created a custom tab page, and dropped a richtextbox onthat tab page. But when instatiated the custom tab page, the richtextbox never showed up. After an exhausted search on the problem, I never found an answer, so I tried a different approach. I create my own class and derived it from TabPage. Then i created a richtextbox manually. I had to laugh when the class appeared the same way in the IDE (designer page magically appeared), but it worked. I saw my richtextbox with no issues. The two tab pages (my manually generated and the article generated) look exactly the same way to the IDE, but the MSDN article generated code does not work.
| | Rick Kirch Thursday, September 27, 2007 5:30 PM | I decided to try to do the same thing, and stumbled across this post before I solved it. Here's what I did.
You need to make sure that all of the controls on the inhereted tab page are correctly setup.
On a normal form / control this is done by the InitializeComponent method, though I'm sure you can do this anywhere. If you use the InitializeComponent method then you need to make sure it is called by all constructors.
For instance (Visual Basic):
Code Block
< Global.Microsoft.VisualBasic.CompilerServices.DesignerGenerated()> _
Partial Class CustomControl1
Inherits System.Windows.Forms.TabPage
Public Sub New()
Me.InitializeComponent()
End Sub
'Control overrides dispose to clean up the component list.
<System.Diagnostics.DebuggerNonUserCode()> _
Protected Overrides Sub Dispose(ByVal disposing As Boolean)
Try
If disposing AndAlso components IsNot Nothing Then
components.Dispose()
End If
Finally
MyBase.Dispose(disposing)
End Try
End Sub
'Required by the Control Designer
Private components As System.ComponentModel.IContainer
' NOTE: The following procedure is required by the Component Designer
' It can be modified using the Component Designer. Do not modify it
' using the code editor.
<System.Diagnostics.DebuggerStepThrough()> _
Friend Sub InitializeComponent()
components = New System.ComponentModel.Container()
Me.TextBox1 = New System.Windows.Forms.TextBox
Me.SuspendLayout()
'
'TabPage1
'
Me.Controls.Add(Me.TextBox1)
'
'TextBox1
'
Me.TextBox1.Location = New System.Drawing.Point(6, 6)
Me.TextBox1.Name = "TextBox1"
Me.TextBox1.Size = New System.Drawing.Size(100, 20)
Me.TextBox1.TabIndex = 0
End Sub
Friend WithEvents TextBox1 As System.Windows.Forms.TextBox
End Class
This required fairly extensive hand modification. How I got it was to generate the tab in the designer as part of a throw away form. Then I copied out the pieces I needed and modified them to work within the inherited class.
IsomerX | | IsomerX Tuesday, October 30, 2007 3:25 PM | I chased down Mick's second suggestion for a while. It worked, but the annoyance of switching the custom tab's base class to UserControl when editing was a bit much. Note that you can pirate apage from another TabControl. This is the path I settled on. Create a new custom UserControl and place a TabControl on it. !-Ensureyour TabControl class is public, or can be otherwise accessed-! Now create a collection of tab pages on the new control. From there you can load them in bulk, like this:
UserControl1 control = new UserControl1();
foreach (TabPage page in control.tabControl1.TabPages)
tabControl.TabPages.Add(page);<br/>
Or, if you set access to the individual tab pages public, you can add them directly.
UserControl1 control = new UserControl1();
tabControl.TabPages.Add(control.tabPage1);
I'm using this to dynamically switch in collections of tab pages based on user selection, so I have several user controls that I pass into a single tab loading method. | | rickfoster Friday, July 03, 2009 6:35 PM |
|