ComboBox CodeSample - Populating with KeyValuePair objects

From Visual WebGui Wiki

Jump to: navigation, search


Overview

An example of how to populate a ComboBox with KeyValuePair objects. The example is presented as a new class, inheriting from combobox, supplying a customized sort function.

VB.NET Code

Imports Gizmox.WebGui.Forms
Public Class KVPComboBox
    Inherits ComboBox
    Private uList As List(Of KeyValuePair(Of String, String)) = New List(Of KeyValuePair(Of String, String))
 
    Sub New()
        MyBase.New()
        ReloadData()
    End Sub
 
    Friend Sub ReloadData()
 
        uList.Add(New KeyValuePair(Of String, String)("001", "Tommy twodime"))
        uList.Add(New KeyValuePair(Of String, String)("002", "Jhonny boy"))
        uList.Add(New KeyValuePair(Of String, String)("003", "John Doe"))
        uList.Add(New KeyValuePair(Of String, String)("004", "Homer Simpson"))
        uList.Add(New KeyValuePair(Of String, String)("005", "Superman"))
 
        uList.Sort(AddressOf CompareKVP)
        Me.DataSource = uList
        Me.ValueMember = "Key"
        Me.DisplayMember = "Value"
    End Sub
 
    Private Shared Function CompareKVP( _
       ByVal x As KeyValuePair(Of String, String), ByVal y As KeyValuePair(Of String, String)) As Integer
 
        Return x.Value.CompareTo(y.Value)
 
    End Function
 
End Class

C# Code

Personal tools