Results 1 to 6 of 6

Thread: fill array from Combobox

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    2

    fill array from Combobox

    I am currently writing a program which I cant seem to quite finish.. I fill a combobox with multiple lines, consisting of 5 elements delimited by commas. I then am trying to write them separately to a text file which I am using in another program. I think the best way to do that is to store the combobox contents to an array then to a text file.

    currently looks like this in the combo box

    Bagatelle, Beethoven, Piano Sonata, THIRD, 7
    Seven Garden, Motzart, String Symphony, FIFTH, 2



    I need it to look like this in the text file

    Bagatelle
    Beethoven
    Piano Sonata
    THIRD
    7
    Seven Garden
    Motzart
    String Symphony
    fifth
    2

  2. #2
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: fill array from Combobox

    Welcome to Forums!

    Would this work for you?
    Code:
    Private Sub Command1_Click()
    Dim i As Integer
    Dim arItems() As String
    Dim strOutput As String
    
        For i = 0 To Combo1.ListCount - 1
            strOutput = strOutput & Combo1.List(i)
            If i < Combo1.ListCount Then
                strOutput = strOutput & ","
            End If
        Next i
        
        arItems = Split(strOutput, ",")
        
        Open "c:\test.txt" For Output As #1
            For i = 0 To UBound(arItems)
                Print #1, Trim(arItems(i))
            Next i
        Close #1
        
        MsgBox "File's done."
    
    End Sub

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: fill array from Combobox

    Welcome to the forums.

    May I ask why you are using a single column control like a combo box to store multiple columns? This seems more like a job for a grid control or a Listview control.

  4. #4

    Thread Starter
    New Member
    Join Date
    Mar 2008
    Posts
    2

    Re: fill array from Combobox

    Listcount is not a member of the combobox, at least not in VB2005, nor is open, Im new to 2005 and am not transitioning well.

  5. #5
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: fill array from Combobox

    Quote Originally Posted by joshboski
    Listcount is not a member of the combobox, at least not in VB2005, nor is open, Im new to 2005 and am not transitioning well.
    Moved to VB.NET

  6. #6
    Hyperactive Member gnaver's Avatar
    Join Date
    Jul 2005
    Location
    Denmark/Sweden
    Posts
    289

    Re: fill array from Combobox

    this does what you want

    Code:
    Using sw As IO.StreamWriter = New IO.StreamWriter("c:\Test.txt")
                For Each Item As String In ComboBox1.Items
                    Dim tmp() As String = Item.Split(",")
                    For Each SplittedString As String In tmp
                        sw.WriteLine(SplittedString.Trim)
                    Next
                Next
            End Using

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width