我试图保存并加载一些列表框,但它似乎并没有起作用。这就是我想要做的事情:
Private Sub saveTransaction_Click(sender As Object, e As EventArgs) Handles saveTransaction.Click
alltransactions.amountList.Items.Add(amountBox.Text)
alltransactions.dateList.Items.Add(dateBox.Text)
alltransactions.idList.Items.Add(idBox.Text)
alltransactions.serverList.Items.Add(serverBox.Text)
My.Settings.AmountList.Add(amountBox.Text)
My.Settings.DateList.Add(dateBox.Text)
My.Settings.IDList.Add(idBox.Text)
My.Settings.ServerList.Add(serverBox.Text)
End Sub
这里是它的加载部分:(只是尝试加载一个,但它甚至不起作用)
Private Sub alltransactions_Load(sender As Object, e As EventArgs) Handles MyBase.Load
amountList.Text = My.Settings.AmountList.ToString
End Sub
这是我遇到的问题。如果我在添加1之前尝试加载allTransactions.vb,我会将Object rederence设置为对象的实例。我假设这是因为我甚至没有设置一个,所以很公平。但如果我保存它:
Additional information: Object reference not set to an instance of an object.
^当我在所有方框中保存我展示的第一个代码时,就会发生这种情况。有小费吗?感谢
答案 0 :(得分:1)
我不确定你想要什么。但是,例如,如果要保存ListBox1的项目,可以将其保存在文本文档中,如下所示:
IO.File.WriteAllLines("ListBox1_Contents.txt", ListBox1.Items.Cast(Of String).ToArray())
要在ListBox1中再次加载文件,您可以使用以下代码:
ListBox1.Items.Clear()
ListBox1.Items.AddRange(IO.File.ReadAllLines("ListBox1_Contents.txt"))
答案 1 :(得分:0)
如果使用Specialized.StringCollection
,您需要在使用之前检查它是否已被初始化,如果在这种情况下这是正在使用的类型。
If My.Settings.AmountList Is Nothing Then
My.Settings.AmountList = New Specialized.StringCollection
End If
加载ListBox项目
My.Settings.AmountList.AddRange(ListBox1.Items.Cast(Of String).ToArray)
或TextBox
My.Settings.AmountList.Add(TextBox1.Text)
自从我们初始化集合以来,上述工作将起作用。希望这会有所帮助。
答案 2 :(得分:0)
以下是使用My.Settings 的替代方法,如果不对此开放,则不再进一步。
这是另一种方法。声明一个私有变量,指向路径和文件名以从ListBox读取数据。
Public Class Form1
Private ListBoxfileName As String =
IO.Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Names.txt")
通过语言扩展程序加载文件(在我的回复底部)
If IO.File.Exists(ListBoxfileName) Then
ListBox1.Items.LoadFromFile(ListBoxfileName)
End If
保存表格关闭
上的项目Private Sub Form1_FormClosing(
sender As Object, e As FormClosingEventArgs) Handles Me.FormClosing
ListBox1.Items.SaveToFile(ListBoxfileName)
End Sub
现在您需要创建ListBoxExtensions并将其添加到您的项目中,添加以下内容,构建并运行
Public Module ListBoxExtensions
''' <summary>
''' Save unbound items to a file
''' </summary>
''' <param name="sender"></param>
''' <param name="FileName">File to save items too</param>
''' <remarks></remarks>
<System.Runtime.CompilerServices.Extension()>
Public Sub SaveToFile(ByVal sender As ListBox.ObjectCollection, ByVal FileName As String)
System.IO.File.WriteAllLines(FileName,
(From Row In sender.Cast(Of String)() Select Row).ToArray())
End Sub
''' <summary>
''' Save unbound items to a file
''' </summary>
''' <param name="sender"></param>
''' <param name="FileName">File to save items too</param>
''' <param name="Append">Indicates whether new contents should be appended to existing file</param>
''' <remarks></remarks>
<System.Runtime.CompilerServices.Extension()>
Public Sub SaveToFile(ByVal sender As ListBox.ObjectCollection, ByVal FileName As String, ByVal Append As Boolean)
Dim Items = (From Row In sender.Cast(Of String)() Select Row).ToList
If Append Then
If IO.File.Exists(FileName) Then
Dim CurrentFileLines = IO.File.ReadAllLines(FileName).ToList
Items.AddRange(CurrentFileLines)
End If
End If
System.IO.File.WriteAllLines(FileName, Items.ToArray)
End Sub
<System.Runtime.CompilerServices.Extension()>
Public Sub LoadFromFile(ByVal sender As ListBox.ObjectCollection, ByVal FileName As String)
sender.AddRange(IO.File.ReadAllLines(FileName))
End Sub
End Module