如何在 My.Settings 中保存 ComboBox 项目,并使用文本框显示/编辑

发布于 2024-10-22 00:21:19 字数 1670 浏览 0 评论 0原文

我正在尝试执行以下操作:

  1. 将 ComboBox 中的项目存储在 My.Settings 中(数据类型并不重要,但需要建议)。
  2. 检索这些项目以在表单加载时填充组合框。
  3. 还在 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:

  1. Store items from ComboBox in My.Settings (datatype doesn't matter, but need suggestions).
  2. Retrieve these items to populate a ComboBox on formload.
  3. 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦过后 2024-10-29 00:21:19

您可以使用 StringCollection 类型进行设置,您可能需要在代码中使用以下导入语句才能使 StringCollection 可用:Imports System.Collections.Specialized

然后您可以使用此 StringCollection 作为 DataSource组合框。

编辑:在您的代码中看到您已经使用了 StringCollection。好的。现在你为什么不像这样访问你的设置呢?

My.Settings.Items = itemCollection

这样您就可以确保不会犯输入错误,并且还可以确保您的设置确实存在。您是否还尝试单步执行代码来检查您的设置是否实际保存?

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?

My.Settings.Items = itemCollection

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文