vb.net 数组到组合框?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14543137/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Array to combo box?
提问by mossypne
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?
我希望能够获取数组的 strCusNum 并用它填充一个组合框,但无法弄清楚如何。有什么帮助吗?
回答by Mark Hall
You can also override the ToStringMethod 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'sDataSource
ToString如上所述,您还可以覆盖结构中的方法。我还创建了一个List(Of CustomerAccountsRec)使添加值更容易的方法,然后我将列表绑定到ComboBox's数据源
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
回答by spajce
If I understand your question.
如果我理解你的问题。
The actual result of Public strCusArray() As CustomerAccountsRecis null, so we can't use this to add the all items from CustomerAccountsRecto ComboBox
的实际结果Public strCusArray() As CustomerAccountsRec是空的,所以我们不能用它来添加从CustomerAccountsRec到 的所有项目ComboBox
To Listall the item from your Structurewe need to use the System.Reflection Namespace
对于您的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
Source: C# version
来源:C# 版本
回答by omittones
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.
您可以使用 ComboBox.Items.Add 方法添加项目,为了正确显示结构,您必须覆盖它的 ToString 方法。
See:
看:
ComboBox: Adding Text and Value to an Item (no Binding Source)
回答by Jason Tyler
You can use LINQ to get an array containing the items you want to display, and then bind that array to the ComboBox.
您可以使用 LINQ 获取包含要显示的项目的数组,然后将该数组绑定到ComboBox.
Dim combo as New ComboBox
combo.DataSource = strCusArray.Select(Function(f) f.strCusNum).ToArray()

