Pregunta

Estoy tratando de hacer lo siguiente:

  1. Almacene los elementos de Combobox en My.settings (el tipo de datos no importa, pero necesita sugerencias).
  2. Recupere estos elementos para completar un Combobox en Formload.
  3. También muestre estos elementos (1 elemento por línea) en el cuadro de texto, donde puedo editar y guardar las ediciones tanto en mis.

Estoy un poco perdido, ¿cómo debo hacer esto?

Código existente:

    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

Pero este código no guardará los valores correctamente, ni los mostrará correctamente en el Combobox o el cuadro de texto.

¿Fue útil?

Solución

Puede usar un tipo de StringCollection para su configuración, es posible que necesite la siguiente instrucción de importaciones en su código para que el StringCollection esté disponible: Imports System.Collections.Specialized

Luego puede usar esta StringCollection como DataSource para el Combobox.

Editar: Saw en su código que ya usa StringCollection. Bien. Ahora, ¿por qué no accedes a tu configuración así?

My.Settings.Items = itemCollection

De esta manera, está seguro de no cometer un error de escritura, y también está seguro de que realmente existe. ¿También intentó pasar el código para verificar si su configuración está realmente guardada o no?

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top