Combobox Dinamis Menggunakan fungsi Collections Generic IEnumerable

Modul1

Module Module1
    'Coded By Shabirin | shabirin@yahoo.co.id
    Public Sub FillComboBox(ByVal cmbBox As ComboBox, ByRef content As Collections.Generic.IEnumerable(Of MyList))
        Dim FillList As New List(Of MyList)
        FillList.AddRange(content)
        With cmbBox
            .DataSource = FillList
            .DropDownStyle = ComboBoxStyle.DropDownList
            .DisplayMember = "DisplayMember"
            .ValueMember = "ValueMember"
        End With
    End Sub
End Module

Public Class MyList
    'Coded By Shabirin | shabirin@yahoo.co.id
    Private strDisplay
    Private strValue

    Public ReadOnly Property DisplayMember()
        Get
            Return strDisplay
        End Get
    End Property

    Public ReadOnly Property ValueMember()
        Get
            Return strValue
        End Get
    End Property

    Public Sub New(ByVal DisplayValue As String, ByVal MemberValue As String)
        strDisplay = DisplayValue.ToString
        strValue = MemberValue.ToString
    End Sub

    Public Sub New(ByVal DisplayValue As Integer, ByVal MemberValue As Integer)
        strDisplay = Convert.ToInt32(DisplayValue)
        strValue = Convert.ToInt32(MemberValue)
    End Sub

    Public Sub New(ByVal DisplayValue As String, ByVal MemberValue As Integer)
        strDisplay = DisplayValue.ToString
        strValue = Convert.ToInt32(MemberValue)
    End Sub

    Public Sub New(ByVal DisplayValue As Integer, ByVal MemberValue As String)
        strDisplay = Convert.ToInt32(DisplayValue)
        strValue = MemberValue.ToString
    End Sub

End Class

Contoh Penggunaan Misalnya pada form1.
buat 2 buah combobox

Public Class Form1
    'Coded By Shabirin | shabirin@yahoo.co.id

    Dim IsiCombo As Collections.Generic.IEnumerable(Of MyList) = New MyList() { _
    New MyList("Merah", 1), _
    New MyList("Kuning", 2), _
    New MyList("Hijau", 3)
    }

    Dim IsiBuku As Collections.Generic.IEnumerable(Of MyList) = New MyList() { _
    New MyList("Buku1", "KodeX1"), _
    New MyList("Buku2", "KodeX2"), _
    New MyList("Buku3", "KodeX3")
    }

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        FillComboBox(ComboBox1, IsiCombo)
        FillComboBox(ComboBox2, IsiBuku)
    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        MsgBox("Combobox1.Text = " & ComboBox1.Text & " dan Combobox1.SelectedValue = " & ComboBox1.SelectedValue)
        MsgBox("Combobox2.Text = " & ComboBox2.Text & " dan Combobox2.SelectedValue = " & ComboBox2.SelectedValue)
    End Sub

End Class

Download SampleCode menggunakan VB2010

  1. Belum ada trackback.

Tinggalkan Balasan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Ubah )

Twitter picture

You are commenting using your Twitter account. Log Out / Ubah )

Facebook photo

You are commenting using your Facebook account. Log Out / Ubah )

Connecting to %s

Ikuti

Get every new post delivered to your Inbox.