VB.NET保存/加载列表框

Kas*_*els 0 vb.net

你好我怎么能加载一个列表框?

有设置

不是文本文件的东西

我试过这个

   Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ListBox1.Text = My.Settings.history
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    My.Settings.history = ListBox1.Text
    My.Settings.Save()
End Sub
Run Code Online (Sandbox Code Playgroud)

但没有运气请帮助

Dan*_*Tao 5

每个WinForms控件都具有Text继承自的属性Control.对于某些控件,例如ListBox,此属性基本上没用.

你可能想在项目ListBox.所以给出history一种类型StringCollection并保存/加载它:

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    ' This is in case the history setting isn't initialized. '
    If My.Settings.history IsNot Nothing Then
        For Each item In My.Settings.history
            ListBox1.Items.Add(item)
        Next
    End If
    Timer1.Start()
End Sub

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
    ' Same reasoning as above. '
    If My.Settings.history Is Nothing Then
        My.Settings.history = new StringCollection
    End If

    My.Settings.history.Clear()
    For Each item In ListBox1.Items
        My.Settings.history.Add(item)
    Next
    My.Settings.Save()
End Sub
Run Code Online (Sandbox Code Playgroud)