VB.NET将组合框保存到My.Settings

时间:2014-09-29 13:19:57

标签: vb.net

希望将组合框项目保存到my.settings集合中。我开发了一个webbrowser,一个组合框将成为我的地址栏。我正在尝试保存访问过的网站的历史记录。

我尝试了下面的代码,但它不起作用。它出错“对象引用未设置为对象的实例”:

进入设置为名称添加MyItems,然后选择System.Collections.Specialized.StringCollection作为数据类型。然后onload如下:

For Each i As String In My.Settings.MyItems
    ComboBox1.Items.Add(i)
Next

FormClosing和香港专业教育学院尝试过FormClosed:现在我把它放在一个按钮事件中以保存它以进行测试

My.Settings.MyItems.Clear()
For Each i As String In ComboBox1.Items
    My.Settings.MyItems.Add(i)
Next

1 个答案:

答案 0 :(得分:0)

我非常喜欢这个网站!所以我回来发布正确的代码,将正确保存并加载组合框条目到my.setting!这已被测试为工作!!!

Private Sub Form1_FormClosing(
        sender As Object,
        e As FormClosingEventArgs) Handles Me.FormClosing
        My.Settings.Categories.Clear()
        For Each item In ComboBox1.Items
            My.Settings.Categories.Add(item.ToString)
        Next
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        For Each item In My.Settings.Categories
            ComboBox1.Items.Add(item)
        Next
        ComboBox1.SelectedIndex = 0
    End Sub
    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        ComboBox1.Items.Add(TextBox1.Text)
    End Sub
    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        If ComboBox1.Items.Count > 0 Then
            Dim Index As Int32 = ComboBox1.SelectedIndex
            ComboBox1.Items.RemoveAt(Index)
            If Index - 1 <> -1 Then
                ComboBox1.SelectedIndex = Index - 1
            End If
        End If
    End Sub