Link to home
Start Free TrialLog in
Avatar of Yos Wibowo
Yos Wibowo

asked on

Duplicating a tab page content to another in TabControl control

Hi...
I'm a beginner in VB. Currently I'm using VB in VS 2015. I've search in the internet how to duplicate a tab page content to another in TabControl control. But I ended up on the basic terminology between TabItem and TabPage, which I found it confusing.
Can someone explain this a little bit when to use each object, especially that relates to goal to duplicate the page to another programmatically.

Thanks in advance.
Yos
Avatar of PNRT
PNRT

Hi

I found this to be the easiest way around this problem in VB.Net

Assuming your Tabcontrol is TabControl1

First: add a panel control  (Panel1) to the tab that you wish to duplicate
Second:  Include all the controls that you have in that tab to Panel1

Now add this code to your form -

 Private Sub TabControl1_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles TabControl1.SelectedIndexChanged

        If TabControl1.SelectedIndex = 4 Then  'Substitute 4 for the index of the tab you want to add the controls to
            TabControl1.SelectedTab.Controls.Add(Me.Panel1)
        End If

    End Sub

Open in new window


This will add the controls in Panel1 to whichever Tab you require when that tab is selected
SOLUTION
Avatar of PNRT
PNRT

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Yos Wibowo

ASKER

Hi PNRT,

Thanks for the reply.

I followed your step, but it still doesn't work...

FYI, I use wpfapplication form for my project in VS 2015 (probably this makes the difference).
And as I compiled, error occurs "BC30456 'SelectedTab' is not a member of 'TabControl'."

Any suggestion?

Thanks,
Yos
Avatar of Ark
    Public Shared Function TryCloneElement(Of T)(orig As T) As T
        Try
            Dim s As String = Markup.XamlWriter.Save(orig),
                stringReader As New IO.StringReader(s),
                reader As XmlReader = XmlTextReader.Create(stringReader, New XmlReaderSettings())
            Return DirectCast(Markup.XamlReader.Load(reader), T)
        Catch
            Return DirectCast(DirectCast(Nothing, Object), T)
        End Try
    End Function

Open in new window

Using:
  Private Sub Button1_Click(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles Button1.Click
        Dim newTab = TryCloneElement(TabItem1)
        If Not newTab Is Nothing Then
            TabControl1.Items.Add(newTab)
        End If
    End Sub

Open in new window

yes, you didn't mention that you were using WPF.  I'll take a look.
PS. To replace existing tab with copy:
Dim newTab = TryCloneElement(TabItem1)
If Not newTab Is Nothing Then
   TabControl1.Items.Remove(TabItem2) 'remove second tab
   TabControl1.Items.Insert(1, newTab) 'Insert copy instead of TabItem2
End If

Open in new window

SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Ark,

I'd prefer to use UserControl as it seems more flexible to put other controls in it (thanks for your suggestion...).
I can follow the idea, but when I try to add tab with "newtab.Controls.add(....)", error occurs "BC03456 'control' is not a member of 'TabItem'."
Private Sub MainWindow_Activated(sender As Object, e As EventArgs) Handles Me.Activated
        Dim currDir As String = "E:\Project\Data\"
        Dim filelines() As String = File.ReadAllLines(currDir & "Region.txt")
        Dim newtab As New TabItem

        newtab.control.add 'error occurs : BC03456 'control' is not a member of 'TabItem'
    End Sub

Open in new window

Is there anything that I missed?

Regards,
Yos
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks, Ark & PNRT for your guidance...really helpful...
I'm learning from you guys...

Yos