Search code examples
vb.netbuttonpersist

How to save dynamically added button for next run in vb.net


I have written code to add a button based on the number of buttons the user needs to add to the form. I am having a very difficult time figuring out how to save the buttons that have been added during runtime and have them persist to the next run. I tried using My.Settings, but have had no luck. Any help would be appreciated! Below is the code I am using to add button(s).

Private Sub UploadNew_Click(sender As Object, e As EventArgs) Handles UploadNew.Click

    Num = InputBox("How many lines to add?")

    Dim pt As Point

    pt.X = 9
    pt.Y = 67

    For x = 1 To Num
        DocName = InputBox("What is the name of the document?")
        Buttons(x) = New Button
        Buttons(x).Location = pt
        Buttons(x).Height = 35
        Buttons(x).Width = 434
        Buttons(x).Text = DocName 'assigned earlier in code
        SplitContainer1.Panel1.Controls.Add(Buttons(x))
        pt.Y = pt.Y + 43

    Next

End Sub

Solution

  • I use the binary serialization. It probably could be done with XML serialization also. The trick is to mark the class as <Serializable>You might think we could just serialize the button but that is not marked as serializable in the framework.

    Imports System.IO
    Imports System.Runtime.Serialization.Formatters.Binary
    
    Public Class SaveDynamicButtons
        Private Buttons As New List(Of MyButtonSettings)
    
        Private Sub SaveDynamicButtons_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            If Not File.Exists("ButtonList.dat") Then
                Exit Sub
            End If
            Dim myList As List(Of MyButtonSettings)
            ' 1. Create an instance of Binary Formatter
            Dim bf As New BinaryFormatter()
            ' 2. Get an instance of a FileStream for the OpenRead method of File passing in (the fileName)
            Using fs As Stream = File.OpenRead("ButtonList.dat")
                '3. cast back to original object, the Deserialize method of the Binary Formatter passing in the File Stream
                myList = (CType(bf.Deserialize(fs), List(Of MyButtonSettings)))
            End Using
            For Each item In myList
                Dim btn As New Button
                btn.Location = item.Location
                btn.Height = 35
                btn.Width = 434
                btn.Text = item.Text
                Me.Controls.Add(btn)
            Next
        End Sub
    
        Private Sub AddButtons()
            Dim Num As Integer
            If Not Integer.TryParse(InputBox("How many lines to add?"), Num) Then
                MessageBox.Show("Please enter a valid number")
                Exit Sub
            End If
            Dim pt As Point
            pt.X = 9
            pt.Y = 67
            For x = 1 To Num
                Dim DocName As String = InputBox("What is the name of the document?")
                Dim btn As New Button
                Dim myBtn As New MyButtonSettings
                btn.Location = pt
                myBtn.Location = pt
                btn.Height = 35
                btn.Width = 434
                btn.Text = DocName
                myBtn.Text = DocName
                Me.Controls.Add(btn)
                Buttons.Add(myBtn)
                pt.Y = pt.Y + 43
            Next
        End Sub
    
        Private Sub SaveButtons()
            Dim bf As BinaryFormatter = New BinaryFormatter()
            'FileStream(FileName, Mode, Access, Share) intellisense overload #9 out of 15
            Using fs As Stream = New FileStream("ButtonList.dat", FileMode.Create, FileAccess.Write, FileShare.None)
                bf.Serialize(fs, Buttons)
            End Using
        End Sub
    
        Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click
            AddButtons()
        End Sub
    
        Private Sub btnSave_Click(sender As Object, e As EventArgs) Handles btnSave.Click
            SaveButtons()
        End Sub
    End Class
    
    <Serializable>
    Public Class MyButtonSettings
        Public Property Location As Point
        Public Property Text As String
    End Class