Vb.net 将字符串保存到设置文件System.Collection.Specialized.StringCollection不起作用

Vb.net 将字符串保存到设置文件System.Collection.Specialized.StringCollection不起作用,vb.net,Vb.net,使用VS2017。当我调用PrintDialog按钮时,我想将一些数据保存到用户设置文件中。但我当然不希望文件中有任何重复数据。下面的操作有效,但只有当我关闭应用程序时,我才希望它在DialogResult上运行。是 ... ElseIf result = DialogResult.Yes Then ' Save the entered data and continue the print 'MessageBox.Show("Yes pressed")

使用VS2017。当我调用PrintDialog按钮时,我想将一些数据保存到用户设置文件中。但我当然不希望文件中有任何重复数据。下面的操作有效,但只有当我关闭应用程序时,我才希望它在DialogResult上运行。是

...
    ElseIf result = DialogResult.Yes Then ' Save the entered data and continue the print
        'MessageBox.Show("Yes pressed")
        If Not String.IsNullOrEmpty(cbPayToo.Text) Or Me.cbPayToo.Text = "" Then
            If Not cbPayToo.Items.Contains(cbPayToo.Text) Then 'make sure the item to save does not exist
                Dim strings(cbPayToo.Items.Count - 1) As String
                cbPayToo.Items.CopyTo(strings, 0)
                My.Settings.cbPayToo.Insert(0, cbPayToo.Text)
            End If
        End If
        If Not String.IsNullOrEmpty(cbCheckAmount.Text) Or Me.cbCheckAmount.Text = "" Then
            If Not cbCheckAmount.Items.Contains(strCheckAmount) Then 'make sure the item to save does not exist
                Dim strings(cbCheckAmount.Items.Count - 1) As String
                cbCheckAmount.Items.CopyTo(strings, 0)
                My.Settings.cbCheckAmount.Insert(0, strCheckAmount)
            End If
        End If
        If Not String.IsNullOrEmpty(cbMemoBox.Text) Or Me.cbMemoBox.Text = "" Then
            If Not cbMemoBox.Items.Contains(cbMemoBox.Text) Then 'make sure the item to save does not exist
                Dim strings(cbMemoBox.Items.Count - 1) As String
                cbMemoBox.Items.CopyTo(strings, 0)
                My.Settings.cbMemoBox.Insert(0, cbMemoBox.Text)
            End If
        End If
    ....
    End If
我在关闭已安装的应用程序时运行此程序,它将正确写入设置

Private Sub Form1_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
    My.Settings.DateBox = DateBox.Location
    My.Settings.CheckToName = cbPayToo.Location
    My.Settings.DollarAmount = cbCheckAmount.Location
    My.Settings.pbSig = pbSig.ImageLocation
    My.Settings.MemoBox = cbMemoBox.Location
End Sub

奇怪的是,当我运行FormClosing时,设置文件会用cb数据更新。运行时我不能更新设置文件吗?

这是因为在项目设置>应用程序中,您选中了“关机时保存我的.Settings”

添加此项以手动保存

My.Settings.Save()

在所有if语句之后,保存设置:

 ElseIf result = DialogResult.Yes Then ' Save the entered data and continue the print
    'MessageBox.Show("Yes pressed")
    If Not String.IsNullOrEmpty(cbPayToo.Text) Or Me.cbPayToo.Text = "" Then
        If Not cbPayToo.Items.Contains(cbPayToo.Text) Then 'make sure the item to save does not exist
            Dim strings(cbPayToo.Items.Count - 1) As String
            cbPayToo.Items.CopyTo(strings, 0)
            My.Settings.cbPayToo.Insert(0, cbPayToo.Text)
        End If
    End If
    If Not String.IsNullOrEmpty(cbCheckAmount.Text) Or Me.cbCheckAmount.Text = "" Then
        If Not cbCheckAmount.Items.Contains(strCheckAmount) Then 'make sure the item to save does not exist
            Dim strings(cbCheckAmount.Items.Count - 1) As String
            cbCheckAmount.Items.CopyTo(strings, 0)
            My.Settings.cbCheckAmount.Insert(0, strCheckAmount)
        End If
    End If
    If Not String.IsNullOrEmpty(cbMemoBox.Text) Or Me.cbMemoBox.Text = "" Then
        If Not cbMemoBox.Items.Contains(cbMemoBox.Text) Then 'make sure the item to save does not exist
            Dim strings(cbMemoBox.Items.Count - 1) As String
            cbMemoBox.Items.CopyTo(strings, 0)
            My.Settings.cbMemoBox.Insert(0, cbMemoBox.Text)
        End If
    End If
    'Add this line to save the settings Immediately
    My.Settings.Save()
    'To update the settings without saving them , you can use My.Settings.Update()
....
End If

My.Settings.Save()
作为表单关闭方法的最后一行。@LArsTech“以下操作有效,但只有当我关闭应用程序时”,他已经在关闭时保存了它,你所要做的就是在关闭时保存两次,他想在关闭时保存,而不是在关闭时保存。在所有if语句之后,使用
My.Settings.Save()