0

我有这个结构,带有结构类型的数组。

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

我希望能够获取数组的 strCusNum 并用它填充一个组合框,但不知道如何。有什么帮助吗?

4

4 回答 4

3

如前所述,您还可以覆盖ToString结构中的方法。我还创建了一个List(Of CustomerAccountsRec)可以更轻松地添加值的方法,然后将列表绑定到ComboBox'sDataSource

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
于 2013-01-27T00:53:54.847 回答
0

您可以使用 LINQ 获取包含要显示的项目的数组,然后将该数组绑定到ComboBox.

Dim combo as New ComboBox
combo.DataSource = strCusArray.Select(Function(f) f.strCusNum).ToArray()
于 2013-01-27T00:45:43.647 回答
0

如果我理解你的问题。

的实际结果Public strCusArray() As CustomerAccountsRec是 null,所以我们不能使用它来添加所有项目 from CustomerAccountsRecto ComboBox

对于List您的所有项目,Structure我们需要使用System.Reflection 命名空间

  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

来源:C#版本

于 2013-01-27T01:31:55.547 回答
0

您可以使用 ComboBox.Items.Add 方法添加项目,并且要正确显示结构,您必须覆盖它的 ToString 方法。

看:

ComboBox:向项目添加文本和值(无绑定源)

如何为组合框项添加值

于 2013-01-27T00:39:02.817 回答