如何为 vb.net 中的下一个 运行 保存动态添加的按钮
How to save dynamically added button for next run in vb.net
我编写了代码,根据用户需要添加到表单的按钮数量来添加按钮。我很难弄清楚如何保存在 运行 时间内添加的按钮并让它们持续到下一个 运行。我尝试使用 My.Settings,但没有成功。任何帮助,将不胜感激!下面是我用来添加按钮的代码。
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
我使用二进制序列化。它可能也可以通过 XML 序列化来完成。诀窍是将 class 标记为 <Serializable>
您可能认为我们可以序列化按钮,但在框架中未标记为可序列化。
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
我编写了代码,根据用户需要添加到表单的按钮数量来添加按钮。我很难弄清楚如何保存在 运行 时间内添加的按钮并让它们持续到下一个 运行。我尝试使用 My.Settings,但没有成功。任何帮助,将不胜感激!下面是我用来添加按钮的代码。
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
我使用二进制序列化。它可能也可以通过 XML 序列化来完成。诀窍是将 class 标记为 <Serializable>
您可能认为我们可以序列化按钮,但在框架中未标记为可序列化。
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