vb.net 从列表到组合框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18960008/
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
from list to combobox
提问by Brian Stallter
My goal is to take user inputted Column information on one form and put it in a combobox on form 2. Im really new to coding and vb.net, please keep it for dummies. This is what I have so far on one form.
我的目标是将用户输入的列信息放在一个表单上,并将其放入表单 2 的组合框中。我对编码和 vb.net 真的很陌生,请保留它以备不时之需。这是我迄今为止在一种形式上的内容。
Public Class CrewDatabaseForm
Public Shared CrewList As List(Of String)
Private Sub CrewDatabaseForm_FormClosed(sender As Object, e As FormClosedEventArgs) Handles Me.FormClosed
Dim CL As New List(Of String)
Dim CLPure As New List(Of String)
For i As Integer = 0 To DataGridView1.RowCount - 1
CL.Add(DataGridView1.Rows(i).Cells(0).Value.ToString)
Next
CLPure = CL.Distinct
CrewList = CLPure
End Sub
Private Sub CrewDatabaseForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
End Sub
End Class
DataGridView1 is just a DataGrid I dragged from the toolbox. Im trying to pass the first column into a list, make it distinct, then pass it to a public list for this form to get a hold of:
DataGridView1 只是我从工具箱中拖出的一个 DataGrid。我试图将第一列传递到列表中,使其不同,然后将其传递到此表单的公共列表中以获取:
Public Class NewWellForm
Private Sub NewWellForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.Add(CLPure.Item(1))
End Sub
End Class
Here Im trying to put the list into Combobox1
在这里,我试图将列表放入 Combobox1
The CLPure here is underlined swith squigglies so it will not work. What am I doing wrong? Also, I keep finding myself struggling to convert from list to array to combobox to column to etc. Is there any good consistency, syntax, or resource for learning how to convert between these dataholders?
此处的 CLPure 带有下划线和波浪线,因此它不起作用。我究竟做错了什么?此外,我一直发现自己在从列表到数组到组合框到列到等等的转换中遇到了困难。是否有任何良好的一致性、语法或资源来学习如何在这些数据持有者之间进行转换?
Thanks
谢谢
回答by Steven Doggart
Since your CrewListfield is Shared, you should be able to access it from the other form like this:
由于您的CrewList字段是Shared,您应该能够从其他表单访问它,如下所示:
Private Sub NewWellForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ComboBox1.Items.AddRange(CrewDatabaseForm.CrewList.ToArray())
End Sub
The AddRangemethod allows you to add multiple items at once, but it needs you to give it an array. Since CrewListis a List(Of String), rather than array, you need to convert it to an array before giving it to the AddRangemethod. Conveniently, the List(Of String)class has a ToArraymethod which makes the conversion very easy.
该AddRange方法允许您一次添加多个项目,但需要您为其提供一个数组。由于CrewListis a List(Of String),而不是数组,您需要在将其提供给AddRange方法之前将其转换为数组。方便的是,List(Of String)该类具有一种ToArray使转换非常容易的方法。
回答by edilon Junior
Create a list of objects and populate the combobox in load method of the form:
创建一个对象列表并在表单的 load 方法中填充组合框:
Object[] myList = new Object[] {4, 6, 8, 10, 12, 14, 16 };
private void myForm_Load(object sender, System.EventArgs e)
{
comboBox1.Items.AddRange(myList);
}

