0

I have this structure, with an array of the structure type.

Structure CustomerAccountsRec
    Dim strFirstName As String
    Dim strLastName As String
    Dim intAge As Integer
    Dim strAddress As String
    Dim strTown As String
    Dim strPostcode As String
    Dim strCusNum As String
End Structure

Public strCusArray() As CustomerAccountsRec

I want to be able to take the strCusNum of the array and populate a combobox with it but can't figure out how. Any help?

mossypne
  • 15
  • 1
  • 2
  • 6

4 Answers4

3

You can also override the ToString Method in your Structure as mentioned. I also created a List(Of CustomerAccountsRec) that makes it a bit easier to add values and then I bound the list to the ComboBox's DataSource

Public Class Form1
    Structure CustomerAccountsRec
        Dim strFirstName As String
        Dim strLastName As String
        Dim intAge As Integer
        Dim strAddress As String
        Dim strTown As String
        Dim strPostcode As String
        Dim strCusNum As String
        Public Overrides Function ToString() As String
            Return strCusNum
        End Function
    End Structure

    Public strCusArray As List(Of CustomerAccountsRec) = New List(Of CustomerAccountsRec)

    Public Sub New()

        ' This call is required by the designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.

        Dim carec As CustomerAccountsRec = New CustomerAccountsRec
        carec.strFirstName = "Hello"
        carec.strLastName = "World"
        carec.strCusNum = "Hello World"
        carec.strTown = "AnyTown"
        carec.strAddress = "AnyStreet"
        carec.strCusNum = "12345678"
        strCusArray.Add(carec)
        ComboBox1.DataSource = strCusArray

    End Sub
End Class
Mark Hall
  • 53,938
  • 9
  • 94
  • 111
0

You can add the items using the ComboBox.Items.Add method, and for the structure to be properly displayed, you have to override it's ToString method.

See:

ComboBox: Adding Text and Value to an Item (no Binding Source)

how to add value to combobox item

Community
  • 1
  • 1
omittones
  • 847
  • 1
  • 10
  • 23
0

You can use LINQ to get an array containing the items you want to display, and then bind that array to the ComboBox.

Dim combo as New ComboBox
combo.DataSource = strCusArray.Select(Function(f) f.strCusNum).ToArray()
Jason Tyler
  • 1,331
  • 13
  • 26
  • But that way it will only add strings to the ComboBox, not objects. ComboBox.SelectedItem would return selected name, and not selected object. Wouldn't LINQ be slower as well? – omittones Jan 27 '13 at 00:51
  • @omittones From the phrasing of the question, I thought all mossypne wanted was `String`, but I'm not sure. The question was a bit vague. As for speed, LINQ will incur some overhead but it would most likely be negligible. – Jason Tyler Jan 27 '13 at 00:55
  • @JasonTyler Basically i have an edit customer form and i want to show each customers ID in a combo box to allow the user to choose which account to edit. The customer ID is stored in an array of the type CustomerRecordStructure – mossypne Jan 27 '13 at 00:57
  • @mossypne I gotcha. I think Mark Hall's answer should suffice unless you need the `ToString` method for other purposes. My solution probably wouldn't be as good for you because you'd need extra logic to do a lookup when a selection is made (Using the ID to lookup the record). – Jason Tyler Jan 27 '13 at 01:03
0

If I understand your question.

The actual result of Public strCusArray() As CustomerAccountsRec is null, so we can't use this to add the all items from CustomerAccountsRec to ComboBox

To List all the item from your Structure we need to use the System.Reflection Namespace

  Structure CustomerAccountsRec
        Dim strFirstName As String
        Dim strLastName As String
        Dim intAge As Integer
        Dim strAddress As String
        Dim strTown As String
        Dim strPostcode As String
        Dim strCusNum As String
  End Structure

  Dim fi As FieldInfo() = GetType(CustomerAccountsRec).GetFields(BindingFlags.[Public] Or BindingFlags.Instance)
        For Each info As FieldInfo In fi
            ComboBox2.Items.Add(info.Name)
  Next

Source: C# version

Community
  • 1
  • 1
spajce
  • 7,044
  • 5
  • 29
  • 44