none
Extracting values from a data bound combo box RRS feed

  • Question

  • Hi,
    I have a combo box which is databound at run time & I need to iterate through it to get all the values in it (not display items) & then add these to an array or collection.

    how can this be done?

    Many thanks for your help

    Ant
    @nt
    Thursday, June 18, 2009 6:00 AM

Answers

  • Public Class Form1
        Dim myList As New List(Of String)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
            For Each item As String In ComboBox1.Items
                myList.Add(item)
            Next
    
        End Sub
    End Class
    

    or if the items are something like Integers


    Public Class Form1
        Dim myList As New List(Of Integer)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
            For Each item As Integer In ComboBox1.Items
                myList.Add(item)
            Next
         End Sub
    End Class
    
    • Edited by jwavila Thursday, June 18, 2009 6:22 AM edit
    • Marked as answer by A n t Thursday, June 18, 2009 11:21 PM
    Thursday, June 18, 2009 6:18 AM

All replies

  • Public Class Form1
        Dim myList As New List(Of String)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
            For Each item As String In ComboBox1.Items
                myList.Add(item)
            Next
    
        End Sub
    End Class
    

    or if the items are something like Integers


    Public Class Form1
        Dim myList As New List(Of Integer)
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
    
            For Each item As Integer In ComboBox1.Items
                myList.Add(item)
            Next
         End Sub
    End Class
    
    • Edited by jwavila Thursday, June 18, 2009 6:22 AM edit
    • Marked as answer by A n t Thursday, June 18, 2009 11:21 PM
    Thursday, June 18, 2009 6:18 AM
  • Ant

    Never use an UI like a combobox as a data suplier, use the datacomponent which is bound to it.

    Therefore, what did you bound to it.

    (There is a strange misunderstanding about runtime, nothing is done at design time than creating code to run at runtime, therefore everything is runtime)


    Success
    Cor
    Thursday, June 18, 2009 6:19 AM