VB.NET Array – Populate ComboBox

In this tutorial we see how to populate a VB.NET combobox from a string array.

Then we handle teh combobox' selected index changed event and show the item in a messagebox.

Let's go.

Imports System
Imports System.Drawing
Imports System.Windows.Forms
Module Module1

    Private WithEvents comboBox As ComboBox

    Sub Main()
        Dim myForm As Form = New Form() With {.Text = "WinForms ComboBox - Array", .ClientSize = New Size(564, 420)}
        comboBox = New ComboBox With {.Location = New Point(164, 176), .DropDownStyle = ComboBoxStyle.DropDownList}
        Dim nebulae As String() = {"Horse Head", "Black Widow", "Ghost Head", "Cat's Eye", "Elephant's Trunk", "Helix", "Rosette", "Snake", "Bernad 68", "Ant", "Orion", "Butterfly", "Eagle", "Own", "Ring", "Pelican", "Cone", "Flame", "Witch Head", "Bumerang"}
        comboBox.DataSource = nebulae
        myForm.Controls.Add(comboBox)
        Application.Run(myForm)
    End Sub

    Private Sub comboBox_SelectedIndexChanged(ByVal sender As Object, ByVal e As EventArgs) Handles comboBox.SelectedIndexChanged
        MessageBox.Show(comboBox.SelectedItem.ToString())
    End Sub
End Module

Result

Related Posts

Leave a Reply

Your email address will not be published. Required fields are marked *