我正在尝试执行以下操作:
- 将 ComboBox 中的项目存储在 My.Settings 中(数据类型无关紧要,但需要建议)。
- 检索这些项目以在表单加载时填充 ComboBox。
- 还在 TextBox 中显示这些项目(每行 1 个项目),我可以在其中编辑并将编辑保存到 My.Settings 和 ComboBox。
我有点迷茫,我该怎么做?
现有代码:
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Labels.LoadSettings()
txtNumOfLabels.Text = Labels.numOfLabels
cboItem.Items.Clear()
For Each s As String In Labels.items
cboItem.Items.Add(s)
Next
End Sub
Public Shared items As New Specialized.StringCollection
Shared Sub LoadSettings()
Try
items = My.Settings("Items")
Catch ex As Exception
MsgBox(ex.Message, MsgBoxStyle.Exclamation)
End Try
End Sub
Private Sub Options_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each s As String In Labels.items
txtItems.AppendText(s + Environment.NewLine)
Next
End Sub
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim itemCollection As New Specialized.StringCollection
For Each s As String In txtItems.Lines
itemCollection.Add(s)
Next
My.Settings("Items") = itemCollection
My.Settings.Save()
Labels.LoadSettings()
Form1.cboItem.Items.Clear()
For Each s As String In Labels.items
Form1.cboItem.Items.Add(s)
Next
Me.Close()
End Sub
但是此代码不会正确保存值,或在组合框或文本框中正确显示它们。