Imports Stax.UI

Public Class SelectionBox(Of T)
    Private Form As New SelectionBoxForm

    Public Sub AddItem(ByVal text As String, ByVal item As T)
        Form.cbItems.Items.Add(New ListBag(Of T)(text, item))
    End Sub

    Public Sub AddItem(ByVal item As T)
        Form.cbItems.Items.Add(item)
    End Sub

    Public Function Show() As DialogResult
        Form.cbItems.SelectedIndex = 0

        If Form.Text = "" Then
            Form.Text = Application.ProductName
        End If

        Dim ret As DialogResult = Form.ShowDialog

        If TypeOf Form.cbItems.SelectedItem Is ListBag(Of T) Then
            SelecteItemValue = ListBag(Of T).GetValue(Form.cbItems)
        Else
            SelecteItemValue = CType(Form.cbItems.SelectedItem, T)
        End If

        Form.Dispose()
        Return ret
    End Function

    Public WriteOnly Property Text() As String
        Set(ByVal value As String)
            Form.lText.Text = value
        End Set
    End Property

    Public WriteOnly Property Title() As String
        Set(ByVal value As String)
            Form.Text = value
        End Set
    End Property

    Private SelecteItemValue As T

    Public ReadOnly Property SelecteItem() As T
        Get
            Return SelecteItemValue
        End Get
    End Property
End Class