vb.net - 如何保存动态添加的按钮以供下次在 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 序列化来完成。诀窍是将类标记为<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
推荐阅读
- php - PHP foreach 循环不更新值
- python - 使用代理和 Selenium 时 URL 发生变化
- sql - 在 Oracle 中按日期设置行顺序时遇到问题
- java - Spring WebFlux + Security - 我们有“记住我”功能吗?
- c# - Web API HttpPost 方法总是接收 GET
- css - 响应式 YouTube 嵌入式视频设置为特定尺寸,没有黑色边框
- swift - 等待 MessageKit 函数中的另一个函数完成
- django - Django:模型中只有 1 个字段未更新
- java - 可运行的 javafx 应用程序中的 Unirest
- javascript - 从另一个代码库运行 tensorflow