vb.net 将集合中的项目添加到组合框

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14742171/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 12:14:31  来源:igfitidea点击:

Add items from a collection to a combo box

vb.netclasscollectionscombobox

提问by Martin Matus Jr

I have a class that reads from a text file, and adds it to a collection. I'm trying to figure out how to read from the collection to populate a combobox upon loading of the program.

我有一个从文本文件中读取的类,并将其添加到集合中。我试图弄清楚如何在加载程序时从集合中读取以填充组合框。

BillingData Class

BillingData 类

Public ReadOnly Property Clients As Collection
    Get
        Return mClients
    End Get
End Property

Sub New()


    mClientFile = OpenText("clients.txt")

    Dim mClients As New Collection

    While Not mClientFile.EndOfStream
        mClients.Add(mClientFile.ReadLine())
    End While


    mClientFile.Close()

mainForm

主窗体

Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    client = New BillingData()

    Dim i As Integer
    While i < client.Clients.Count
        cbClient.Items.AddRange(client.Clients(i))
        i = i + 1
    End While
End Sub

回答by Mash

I made 3 changes to the frmMain_Loadsubroutine.

我对frmMain_Load子程序进行了 3 次更改。

  1. Initialized counter ito 1.
  2. Changed the while loop condition to <=.
  3. Used Addinstead of AddRange.

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        client = New BillingData()
    
        Dim i As Integer = 1
        While i <= client.Clients.Count
            cbClient.Items.Add(client.Clients(i))
            i = i + 1
        End While
    
    End Sub
    
  1. 将计数器初始化i为 1。
  2. 将 while 循环条件更改为<=.
  3. 用于Add代替AddRange.

    Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
        client = New BillingData()
    
        Dim i As Integer = 1
        While i <= client.Clients.Count
            cbClient.Items.Add(client.Clients(i))
            i = i + 1
        End While
    
    End Sub
    

An interesting thing to note for the first change (initializing i to 1): The value of Clients at index 0 is "empty placeholder for a 1-based array". This basically means the collection starts at index 1. The code will sort of throw a phantom exception that never gets caught - this is typical in VB.net form load routines. You can test this out by setting a breakpoint in your load code and see that it never gets to the line i = i + 1. If you placed your code in a button click event instead, you would see the code break on the exception. Moral of the story is to be careful on any code that you put on form load routines, because you do not always get immediate feedback if there is a bug in the code.

对于第一个更改(将 i 初始化为 1)要注意的一件有趣的事情:索引 0 处的 Clients 的值是“基于 1 的数组的空占位符”。这基本上意味着集合从索引 1 开始。代码将抛出一个永远不会被捕获的幻影异常 - 这在 VB.net 表单加载例程中是典型的。您可以通过在加载代码中设置断点来测试这一点,并查看它永远不会到达该行i = i + 1。如果您将代码放在按钮单击事件中,您将看到异常的代码中断。这个故事的寓意是要小心处理表单加载例程中的任何代码,因为如果代码中存在错误,您并不总是能立即得到反馈。

回答by Mark Hall

You can try adding your Collection to the ComboBox's DataSource. If your problem is that your Collection is not initialized before you add it to the ComboBox you can add an event to your BillingData Class that is raised when the data is ready. You could then add the collection to your ComboBox in the Event Handler.

您可以尝试将您的 Collection 添加到 ComboBox 的DataSource。如果您的问题是在将集合添加到 ComboBox 之前未初始化它,您可以向 BillingData 类添加一个事件,该事件在数据准备好时引发。然后,您可以将集合添加到事件处理程序中的 ComboBox。

cbClient.DataSource = client.Clients