Skip to main content

Vb.net - ListBox


ListBox


ListBox 컨트롤은 Visual Basic .NET(VB.NET)에서 사용하는 리스트 박스 컨트롤입니다. 
이 컨트롤은 항목의 목록을 표시하고, 사용자가 항목을 선택할 수 있도록 합니다.



아래 코드는 Button1_Click 이벤트 핸들러에서 ListBox 컨트롤에 Dictionary(Key, Value)를 이용해 세 개의 항목("ItemName1", "ItemName2", "ItemName3")을 추가하는 코드를 보여줍니다.

Source code


Public Class Form1
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load

    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

        Dim comboSource As New Dictionary(Of String, String)()
        comboSource.Add("ItemName1", "Itemkey1")
        comboSource.Add("ItemName2", "Itemkey2")
        comboSource.Add("ItemName3", "Itemkey3")

        With ListBox1
            .Items.Clear()
            .DataSource = New BindingSource(comboSource, Nothing)
            .DisplayMember = "Key"
            .ValueMember = "Value"
        End With
        comboSource = Nothing
        ListBox1.SelectedValue = "Itemkey1"     '초기값

    End Sub

    Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click

        MessageBox.Show("Key : " & ListBox1.SelectedValue & " /  " & "Value : " & ListBox1.SelectedValue, "정보", MessageBoxButtons.OK)

    End Sub


End Class