Visual Basic Developer Center > Visual Basic Forums > Visual Basic Language > populate combobox or listbox from two dimensional array
Ask a questionAsk a question
 

Questionpopulate combobox or listbox from two dimensional array

  • Thursday, May 10, 2007 7:44 PMMike Doyle Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Hi there,
    I have been trying for hours to populate a listbox (I would prefer a combobox) from a two dimensional array.  Here is the code I am using now.  It is in the load event of a VB form.


    Code Snippet





            Dim Session As New AccpacCOMAPI.AccpacSession
            Dim OrgCount As Long
            Dim OrgCount01 As System.Int16
            Dim Ids(OrgCount) As Object
            Dim CONames(OrgCount) As Object
            Dim SysIDs(OrgCount) As Object
            Dim CoTypes(OrgCount) As System.Int16
            Dim CoSecurity(OrgCount) As Boolean
            Dim ListOfCompanies(1, 1) As String
            Dim i As Long


            Session = CreateObject("Accpac.Session")
            Session.Init("", "GL", "EP0001", "53B")
            OrgCount = Session.Organizations.Count
            ReDim ListOfCompanies(OrgCount - 1, 1)
            Session.Organizations.GetOrgsInfo(OrgCount01, Ids, CONames, SysIDs, CoTypes, CoSecurity)


            For i = 0 To OrgCount - 1
                ListOfCompanies(i, 0) = Ids(i)
                ListOfCompanies(i, 1) = CONames(i)
            Next i


           ListBox1.Items.AddRange(New Object() {ListOfCompanies})




    The result in the listbox is 'String[,],Array'

    Everything works fine up to the Listbox1.items.addrange line.  ListOfCompanies looks just the way I want it to. 

    Any tips would be appreciated. 

All Replies

  • Thursday, May 10, 2007 8:01 PMkleinmaMVP, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     

    listbox (or combobox) accept full objects into their collections, but obviously all they can actually display, are strings.

     

    So if you add an object to the list using add or addrange, if the object is NOT a string, it calls the objects .ToString() method.

     

    So even though your array is an array of strings, it is still an array, and not itself a string.

     

    So when you add your array object to the list, it calls .ToString() on it, which is why you get the output in the list that you do.

     

     

    what data exactly do you want to be displayed in the listbox (combobox)?

  • Sunday, May 13, 2007 10:51 PMMike Doyle Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    I am displaying text in the combobox; to be exact, two pieces of text data, a code and a name.  The text is contained in an array created in the code.  I may have solved the problem myself, but if you have any easier suggestions, I'm open to them. 

    I created a class to house my data, populated the data from the connection to ACCPAC and then created an array string from it indicating the DispayMember and the ValueMember of the data.  It seems to be working now, but it seems like a lot of code to write in order to use an array as the datasource  in a combo or listbox. 

    MIke