Saving TabControl to a form at runtime

MAK10

New member
Joined
Nov 27, 2011
Messages
3
Programming Experience
Beginner
Hi,

Am new here and also a beginner in VB. I have created a form with a TabControl and when I click the tab page it creates another TabControl Dynamically. This works fine.
I would like to save the form with the newly created TabControl and when I close the form and reload it the new TabControl should still be there.

Is there a way of doing this?

I have tried saving using my.settings but it doesn't work. Any help would be very much appreciated.

Here's the code:

VB.NET:
Public Class Form1


    Inherits System.Windows.Forms.Form

  
    Private Sub Form1_Load(ByVal sender As System.Object,
       ByVal e As System.EventArgs) Handles MyBase.Load
        Me.BackColor = My.Settings.MyBackColor

    End Sub
   

    'CREATE A NEW TAB CONTROL AT RUNTIME

    Private Sub TabPage1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TabPage1.Click
        ' Create a TabControl and set its properties


        Dim dynamicTabControl As New TabControl()
        Dim TabPage As New TabPage()

        dynamicTabControl.Name = "DynamicTabControl"

        dynamicTabControl.BackColor = Color.White

        dynamicTabControl.ForeColor = Color.Black

        dynamicTabControl.Font = New Font("Georgia", 16)

        dynamicTabControl.Width = 300

        dynamicTabControl.Height = 200

        'Add TabPage

        TabPage.Name = "tabPage2"

        TabPage.Text = "Author"

        TabPage.BackColor = Color.Green

        TabPage.ForeColor = Color.White

        TabPage.Font = New Font("Verdana", 12)

        TabPage.Width = 100

        TabPage.Height = 100

        dynamicTabControl.TabPages.Add(TabPage)

        Me.Controls.Add(dynamicTabControl)

        My.Settings.MyTAB = dynamicTabControl
        My.Settings.Save()
    End Sub
    

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        ColorDialog1.ShowDialog()
        My.Settings.MyBackColor = ColorDialog1.Color
    End Sub
End Class

Capture1.PNGCapture2.PNG
 
Last edited:
You don't save the TabControl. You save data that describes the TabControl. At run time, you read that data and use it to build a new TabControl. It's up to you to determine what information you need to describe the control(s) you need to build. Obviously you want it to be as little as possible.
 
Thanks for your reply. I assume you mean to save the control data to My.Settings. How do I do that?

Do I create a string representing the TabControl and put the data in the value of that string in My.Settings?
 
Maybe My.Settings or maybe not. Depending on what you want to save, it may not be the best choice.

You create whatever best describes what you need. If all you need to store is the number of tabs then all you need is an Integer. If what you need is the text of each tab then a StringCollection would do. If you need more data then you will need a more complex data structure to store it. It's up to you to decide what information you need and then create an appropriate data structure to hold it.
 
Maybe My.Settings or maybe not. Depending on what you want to save, it may not be the best choice.

What I am trying to do is finding a way for the user to be able to add a control (A TabControl/a button or a PictureBox)to the form at run time. When they click on a button their form should be saved and when they reload it, the control they created should still be there.

Is it possible? what other ways can this be done. a database? can the form be overwritten?

I understand that I need to create variables for strings and text etc... but what about the actual control design?

As an example, when you use a word document and you add a picture and save it then reload it the document would still contain the picture. How is it done?
 
Back
Top