正在将字符串保存到设置文件System.Collection.Specialized.StringCollection不



使用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数据更新。我可以在运行时不更新设置文件吗?

这是因为在项目设置>应用程序中,您选中了"关闭时保存my.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

相关内容

最新更新