none
como grabar los items de un combobox sin usar archivo .txt RRS feed

  • Pregunta

  • hola tengo este codigo y no me funciona no se porque :

    Option Strict On
    Option Explicit On

    Imports System.IO.Path
    Imports System.Environment
    Public Class Form1
        Private Class ComboBoxControl
            Public Name As String
            Public SelectedIndex As Integer
            Public ItemList As New List(Of String)
        End Class

        Private comboList As New List(Of ComboBoxControl)

        Private programDataFolderPath As String = _
            Combine(GetFolderPath(SpecialFolder.ApplicationData), _
                    My.Application.Info.AssemblyName)

        Private settingsFilePath As String = Combine(programDataFolderPath, "Settings.xml")
        Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

            If ComboBox1.SelectedIndex > -1 Then
                btn_RemoveSelected.Enabled = True
            Else
                btn_RemoveSelected.Enabled = False
            End If
        End Sub

        Private Sub btn_RemoveSelected_Click(sender As System.Object, e As System.EventArgs) Handles btn_RemoveSelected.Click
            If ComboBox1.SelectedIndex > -1 Then
                Dim itemToRemove As String = ComboBox1.Items(ComboBox1.SelectedIndex).ToString

                With ComboBox1
                    .BeginUpdate()
                    .SelectedIndex = -1
                    .Enabled = False

                    For i As Integer = ComboBox1.Items.Count - 1 To 0 Step -1
                        If ComboBox1.Items(i).ToString = itemToRemove Then
                            ComboBox1.Items.RemoveAt(i)
                            Exit For
                        End If
                    Next

                    .EndUpdate()
                End With
            End If

            ComboBox1.Enabled = True
        End Sub

        Private Sub btn_AddItem_Click(sender As System.Object, e As System.EventArgs) Handles btn_AddItem.Click
            Dim itemToAdd As String = TextBox_ItemToAdd.Text.Trim

            If ComboBox1.Items.Contains(itemToAdd) Then
                MessageBox.Show("This item is already in the combobox.", _
                                "Cannot Add Item", MessageBoxButtons.OK, _
                                MessageBoxIcon.Warning)
            Else
                ComboBox1.Items.Add(itemToAdd)
            End If

            ComboBox1.Enabled = True

            With TextBox_ItemToAdd
                .Clear()
                .Focus()
            End With
        End Sub

        Private Sub TextBox_ItemToAdd_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox_ItemToAdd.TextChanged
            If TextBox_ItemToAdd.Text.Trim <> "" Then
                btn_AddItem.Enabled = True
            Else
                btn_AddItem.Enabled = False
            End If

        End Sub

        Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            If e.CloseReason = CloseReason.UserClosing Then
                Dim thisCombo As New ComboBoxControl

                With thisCombo
                    .Name = ComboBox1.Name
                    .SelectedIndex = ComboBox1.SelectedIndex

                    For Each itm As String In ComboBox1.Items
                        .ItemList.Add(itm)
                    Next
                End With

                comboList.Add(thisCombo)

                My.Settings.Save()
            End If
        End Sub

        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load


            If Not My.Computer.FileSystem.DirectoryExists(programDataFolderPath) Then
                My.Computer.FileSystem.CreateDirectory(programDataFolderPath)
            End If

            With ComboBox1
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.ListItems
                .DropDownStyle = ComboBoxStyle.DropDownList
                .Enabled = False
            End With

            btn_AddItem.Enabled = False
            btn_RemoveSelected.Enabled = False

            My.Settings.Reload()
        End Sub
    End Class
    Option Strict On
    Option Explicit On
    
    Imports System.IO.Path
    Imports System.Environment
    Public Class Form1
        Private Class ComboBoxControl
            Public Name As String
            Public SelectedIndex As Integer
            Public ItemList As New List(Of String)
        End Class
    
        Private comboList As New List(Of ComboBoxControl)
    
        Private programDataFolderPath As String = _
            Combine(GetFolderPath(SpecialFolder.ApplicationData), _
                    My.Application.Info.AssemblyName)
    
        Private settingsFilePath As String = Combine(programDataFolderPath, "Settings.xml")
        Private Sub ComboBox1_SelectedIndexChanged(sender As System.Object, e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
    
            If ComboBox1.SelectedIndex > -1 Then
                btn_RemoveSelected.Enabled = True
            Else
                btn_RemoveSelected.Enabled = False
            End If
        End Sub
    
        Private Sub btn_RemoveSelected_Click(sender As System.Object, e As System.EventArgs) Handles btn_RemoveSelected.Click
            If ComboBox1.SelectedIndex > -1 Then
                Dim itemToRemove As String = ComboBox1.Items(ComboBox1.SelectedIndex).ToString
    
                With ComboBox1
                    .BeginUpdate()
                    .SelectedIndex = -1
                    .Enabled = False
    
                    For i As Integer = ComboBox1.Items.Count - 1 To 0 Step -1
                        If ComboBox1.Items(i).ToString = itemToRemove Then
                            ComboBox1.Items.RemoveAt(i)
                            Exit For
                        End If
                    Next
    
                    .EndUpdate()
                End With
            End If
    
            ComboBox1.Enabled = True
        End Sub
    
        Private Sub btn_AddItem_Click(sender As System.Object, e As System.EventArgs) Handles btn_AddItem.Click
            Dim itemToAdd As String = TextBox_ItemToAdd.Text.Trim
    
            If ComboBox1.Items.Contains(itemToAdd) Then
                MessageBox.Show("This item is already in the combobox.", _
                                "Cannot Add Item", MessageBoxButtons.OK, _
                                MessageBoxIcon.Warning)
            Else
                ComboBox1.Items.Add(itemToAdd)
            End If
    
            ComboBox1.Enabled = True
    
            With TextBox_ItemToAdd
                .Clear()
                .Focus()
            End With
        End Sub
    
        Private Sub TextBox_ItemToAdd_TextChanged(sender As System.Object, e As System.EventArgs) Handles TextBox_ItemToAdd.TextChanged
            If TextBox_ItemToAdd.Text.Trim <> "" Then
                btn_AddItem.Enabled = True
            Else
                btn_AddItem.Enabled = False
            End If
    
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            If e.CloseReason = CloseReason.UserClosing Then
                Dim thisCombo As New ComboBoxControl
    
                With thisCombo
                    .Name = ComboBox1.Name
                    .SelectedIndex = ComboBox1.SelectedIndex
    
                    For Each itm As String In ComboBox1.Items
                        .ItemList.Add(itm)
                    Next
                End With
    
                comboList.Add(thisCombo)
    
                My.Settings.Save()
            End If
        End Sub
    
        Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
    
    
            If Not My.Computer.FileSystem.DirectoryExists(programDataFolderPath) Then
                My.Computer.FileSystem.CreateDirectory(programDataFolderPath)
            End If
    
            With ComboBox1
                .AutoCompleteMode = AutoCompleteMode.SuggestAppend
                .AutoCompleteSource = AutoCompleteSource.ListItems
                .DropDownStyle = ComboBoxStyle.DropDownList
                .Enabled = False
            End With
    
            btn_AddItem.Enabled = False
            btn_RemoveSelected.Enabled = False
    
            My.Settings.Reload()
        End Sub
    End Class

    lunes, 31 de marzo de 2014 3:54

Todas las respuestas