如何在 My.Settings 中保存 ComboBox 项目,并使用文本框显示/编辑
我正在尝试执行以下操作:
- 将 ComboBox 中的项目存储在 My.Settings 中(数据类型并不重要,但需要建议)。
- 检索这些项目以在表单加载时填充组合框。
- 还在 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
但是此代码无法正确保存值,也无法在组合框或文本框中正确显示它们。
I'm trying to do the following:
- Store items from ComboBox in My.Settings (datatype doesn't matter, but need suggestions).
- Retrieve these items to populate a ComboBox on formload.
- Also display these items (1 item per line) in TextBox, where I can edit and save the edits to both My.Settings and the ComboBox.
I'm a little lost, how should I go about doing this?
Existing Code:
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
But this code won't save the values properly, or display them properly in the combobox or textbox.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 StringCollection 类型进行设置,您可能需要在代码中使用以下导入语句才能使 StringCollection 可用:
Imports System.Collections.Specialized
然后您可以使用此 StringCollection 作为 DataSource组合框。
编辑:在您的代码中看到您已经使用了 StringCollection。好的。现在你为什么不像这样访问你的设置呢?
这样您就可以确保不会犯输入错误,并且还可以确保您的设置确实存在。您是否还尝试单步执行代码来检查您的设置是否实际保存?
You could use a StringCollection type for your setting, you may need the following imports statement in your code for the StringCollection to be available:
Imports System.Collections.Specialized
You can then use this StringCollection as the DataSource for the combobox.
Edit: Saw in your code that you already use StringCollection. Good. Now why don't you access your setting like this?
This way you're sure not to make a typing mistake, and you're also sure that you setting actually exists. Also did you try stepping through the code to check if your setting is actually saved or not?