ListBox CodeSample - Populating with List of KeyValuePair objects

From Visual WebGui Wiki

Jump to: navigation, search


Overview

This codesample demonstrates how you can populate ListBox by creating a List of KeyValuePair objects and assigning to the DataSource.

VB.NET Code

Imports Gizmox.WebGui.Forms
Public Class KVPListBox
    Inherits ListBox
    Private uList As List(Of KeyValuePair(Of String, String)) = New List(Of KeyValuePair(Of String, String))
    Sub New()
        MyBase.New()
    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
 
' In your form, after instanciating an object of type KVPListBox, in this example you will have to call:
KvpListBox1.ReloadData()

C# Code

Personal tools