Question dynamically adding tabpages with controls

jardoo

New member
Joined
Jan 10, 2011
Messages
4
Programming Experience
1-3
I'm working on a project that will add a new tab to the tabcontrol for every row in an access database.
I can add the tabpages, but how do I get each new tab to include controls on it.
I was thinking I should use a resource dictionary but haven't been able to find any good examples.
I'm not even sure if a resource dictionary is the right way to go.
Any help is appreciated.
 
You should start by designing a UserControl. Once added to the project, you design it just like a form but then you use it just like any other control. You can then create an instance of that UserControl when you create the TabPage and add it to the page's Controls collection.

You can take things a step further and define your own custom TabPage that will automatically create and add the UserControl itself. That way you can also add other members to the TabPage that make it easier to access the data in the UserControl. Check out the following thread to see how I've done this with a WebBrowser control. You can follow the pattern but add your UserControl instead.

Tabbed WebBrowser
 
thanks, that worked I was makeing it more difficult than it really is. createing a usercontrol then assigning it to the tabpage.content was easy.
 
I have a new problem, I want to pass data to the usercontrol on the new tab.
this is what I have on the main window.
VB.NET:
Class MainWindow
    Protected Friend WithEvents mycontrol1 As New UserControl1

    Private Sub server_bakcup_viewer_load(ByVal sender As Object, ByVal e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        Dim cn As New OleDbConnection
        Dim dbProvider As String
        Dim dbsource As String
        Dim tabcontrol1 As New TabControl
        Dim command As OleDbCommand
        Dim sql As String
        Dim rs As OleDbDataReader
        '  Dim mycontrol1 As New UserControl1

        dbProvider = "Provider=Microsoft.jet.oledb.4.0;"
        dbsource = "Data Source=c:\\program files\\server backup viewer\\servers.mdb"

        cn.ConnectionString = dbProvider & dbsource
        cn.Open()

        sql = "select * from server_info"

        command = New OleDbCommand(sql, cn)
        rs = command.ExecuteReader

        Do While rs.Read
            Dim tabpage1 As New TabItem

            tabpage1.Name = "newtab" & rs(1)
            tabpage1.Header = rs(1)

            tabpage1.Content = New UserControl1

            Me.TabControl1.Items.Add(tabpage1)

            mycontrol1.settextboxtext("test")

        Loop

        cn.Close()


    End Sub
this is what I have on the user control
VB.NET:
Public Class UserControl1


    Public Sub settextboxtext(ByVal val As String)

        TextBox1.Text = val
    End Sub
End Class
The project runs but its not adding anything to the textbox on the usercontrol.
 
Last edited by a moderator:
I added formatting tags to your code for you. Please format all code snippets in future for readability.

This is your code:
VB.NET:
        Do While rs.Read
            Dim tabpage1 As New TabItem

            tabpage1.Name = "newtab" & rs(1)
            tabpage1.Header = rs(1)

            tabpage1.Content = New UserControl1

            Me.TabControl1.Items.Add(tabpage1)

            [B][U]mycontrol1.settextboxtext("test")[/U][/B]

        Loop
Where are you setting 'mycontrol1'? Surely that's the UserControl you added to the new TabPage, but you aren't assigning that object to that variable anywhere.
 
Back
Top