-
Oct 17th, 2014, 11:44 AM
#1
Thread Starter
Addicted Member
adding buttons to tab pages at runtime programatically
I would like to be able to add a tab page to my tab control which is in a user control. I can accomplish this by setting a property that returns the tab page collection. However I also want to be able to add one or more buttons to the tab page once it is added to the collection. I have tried doing this in the controladded method with no success. I have also tried placing the code within the property that returns the tab page collection. Also incorrect. Oh, that code location does create buttons, too many buttons! I am obviously missing something but I can't figure out the logic. Help if you can. I would submit the code I've tried but it is lost at the moment. Not erased, just lost in many commented out sections. If you need it, I will try to piece it back together.
All it takes for evil to triumph is for good men to stop programming. (My apologies to Edmund Burke)
-
Oct 17th, 2014, 11:56 AM
#2
Re: adding buttons to tab pages at runtime programatically
Here is how I typically dynamically add tab pages with controls:
Code:
Private Function AddTab() As TabPage
'Create a new instance of the control
Dim newTab As TabPage = New TabPage With {.Text = "Foo"}
'Create any controls
Dim fooLabel As Label = New Label With {.AutoSize = True, .Location = New Point(5, 5), .Text = "FooLabel"}
Dim fooTextBox As TextBox = New TextBox With {.Left = fooLabel.Left, .Top = fooLabel.Bottom + 2, .Width = 150}
Dim fooButton As Button = New Button With {.Left = fooLabel.Left, .Text = "FooButton", .Top = fooTextBox.Bottom + 5}
'Add the controls
newTab.Controls.AddRange({fooLabel, fooTextBox, fooButton})
'Return the page
Return newTab
End Function
Then you'd add the tab like this:
Code:
MyTabControl.Controls.Add(Me.AddTab)
-
Oct 17th, 2014, 08:20 PM
#3
Thread Starter
Addicted Member
Re: adding buttons to tab pages at runtime programatically
I have discovered that this does not work within the framework of property settings. In other words, if I have a property "Tab_Pages" that returns the collection of pages on a tab control, your function does not produce tabs already labelled. Or, it produces duplicates, such as Tab1,Tab1,Tab3,Tab3,Tab5... I'm not sure how to go about correcting this. I normally don't have this problem with properties that return collections, but the tab control is apparently different. I can now show some code so maybe that will help:
Code:
Imports System.Collections.ObjectModel
Imports System.ComponentModel
Imports System.Windows.Forms
Imports System.Windows.Forms.TabControl
Public Class RibbonControl
Dim newTab As TabPage
Dim newButton As Button
Private Function AddTab() As TabPage
newTab = New TabPage With {.Text = "Tab" & TabCtrl.TabCount + 1}
TabCtrl.Controls.Add(newTab)
Return newTab
End Function
Private Function AddButton() As Button
newButton = New Button With {.Name = "Button1", .BackColor = Drawing.Color.White, .Dock = DockStyle.Left, .FlatStyle = FlatStyle.Flat, .Font = New Drawing.Font("Arial", 9), .ForeColor = Drawing.Color.Black, .ImageList = MainImageList, .ImageKey = "", .ImageAlign = Drawing.ContentAlignment.MiddleRight, .Margin = New Padding(0), .TabIndex = 1, .Text = "Button", .TextAlign = Drawing.ContentAlignment.MiddleLeft, .TextImageRelation = TextImageRelation.TextBeforeImage}
newTab.Controls.Add(newButton)
Return newButton
End Function
<Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public ReadOnly Property TabPages() As TabPageCollection
Get
TabCtrl.Controls.Add(Me.AddTab())
Return TabCtrl.TabPages
End Get
End Property
<Browsable(True), DesignerSerializationVisibility(DesignerSerializationVisibility.Content)> _
Public ReadOnly Property TabControls As TabPage.TabPageControlCollection
Get
Return newTab.Controls
newTab.Controls.Add(Me.AddButton())
Return newTab.Controls
End Get
End Property
Private Sub RibbonControl_Load(sender As Object, e As EventArgs) Handles Me.Load
Me.Dock = DockStyle.Top
End Sub
End Class
All it takes for evil to triumph is for good men to stop programming. (My apologies to Edmund Burke)
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|