使用VS2017。当我调用PrintDialog按钮时,我想将一些数据保存到“用户设置”文件中。但是我当然不希望文件中有任何重复数据。以下工作有效,但仅当我关闭该应用程序时,我希望它在DialogResult.Yes上运行。
...
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数据更新了设置文件。我不能在运行时更新设置文件吗?
答案 0 :(得分:1)
这是因为在项目设置>应用程序中,您选中了“在关机时保存我的设置”。
添加它以手动保存。
My.Settings.Save()
答案 1 :(得分:1)
在所有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