Extensibility Question

Posts   
 
    
MarcoP avatar
MarcoP
User
Posts: 270
Joined: 29-Sep-2004
# Posted on: 11-Dec-2004 16:09:20   

I'm creating a control that inherits from a combobox (infragistics 2 be exact). I am implementing most recently used functionality, where the last five entries entered into the combobox are persisted across all control instances in the application and are synced up at all times. All this is working fine and dandy, but I am having trouble coming up with a way to allow this to be fully extendable. I guess the biggest opstacle I see is that I am using a shared, derived queue class to manage the entries and a shared event used internally to allow the controls to allways be notified when to resync their list. I'm sure there is probably an easy way to accomodate this, but I have been looking at this screen way to long. smile

Here is the source:

 Imports Infragistics.Win.UltraWinEditors

Namespace Controls
    ''' -----------------------------------------------------------------------------
    ''' Project    : Sheakley.Evolution.Windows.UI.Controls
    ''' Class        : Evolution.Windows.UI.MRUComboBox 
    ''' 
    ''' Copyright (C) 2004, All Rights Reserved
    ''' -----------------------------------------------------------------------------
    ''' <summary>
    ''' Extends the EvolutionComboBox control in order to add mru list functionality.
    ''' </summary>
    ''' <remarks>
    ''' </remarks>
    ''' <history>
    '''   mpaul   12/10/2004        Created
    ''' </history>
    ''' -----------------------------------------------------------------------------
    Public Class PolicyComboBox
        Inherits UltraComboEditor

        ''' -----------------------------------------------------------------------------
        ''' Project   : Sheakley.Evolution.Windows.UI.Controls
        ''' Class    : Evolution.Windows.UI.Controls.MRUComboBox.MRUQueue
        ''' 
        ''' Copyright (C) 2004, All Rights Reserved
        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Extends the System.Collections.Queue class in order to automatically purge items when the queue is full.
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''   mpaul   12/10/2004        Created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Friend Class MRUQueue
            Inherits System.Collections.Queue

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' Private attributes.
            ''' </summary>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            '''   mpaul   12/10/2004        Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Private _max As Integer

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' Ctor.
            ''' </summary>
            ''' <param name="max">Maximum allowed items in the queue at any given time</param>
            ''' <remarks>
            ''' </remarks>
            ''' <history>
            '''   mpaul   12/10/2004        Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Public Sub New(ByVal max As Integer)
                _max = max
            End Sub

            ''' -----------------------------------------------------------------------------
            ''' <summary>
            ''' Adds the item to the queue.
            ''' </summary>
            ''' <param name="obj">Object to add to the queue</param>
            ''' <remarks>
            ''' Removes the last item added if the maximum allowed items is reached.
            ''' </remarks>
            ''' <history>
            '''   mpaul   12/10/2004        Created
            ''' </history>
            ''' -----------------------------------------------------------------------------
            Public Overrides Sub Enqueue(ByVal value As Object)
                If Me.Count >= _max Then Me.Dequeue()
                MyBase.Enqueue(value)
            End Sub
        End Class

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Private attributes.
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''   mpaul   12/10/2004        Created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Shared _mostRecentlyUsedList As MRUQueue

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Public events.
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''   mpaul   12/10/2004        Created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Shared Event ListChanged(ByVal sender As Object, ByVal e As EventArgs)

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Private shared ctor.
        ''' </summary>
        ''' <remarks>
        ''' This constructor is only called once, the first time an instance of this class is requested.
        ''' </remarks>
        ''' <history>
        '''   mpaul   12/10/2004        Created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Shared Sub New()
            _mostRecentlyUsedList = New MRUQueue(10)
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Ctor.
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''   mpaul   12/10/2004        Created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Sub New()
            MyBase.New()

            ' first thing we need to do is load up our items collection from the shared queue.
            LoadItemsFromQueue()

            MyBase.MaxLength = 8  ' Policy #'s cannot be greater than eight digits. 
            MyBase.DropDownStyle = Infragistics.Win.DropDownStyle.DropDown

            ' wire up our event handler in order to be notified whenever the internal list changes.
            AddHandler Me.ListChanged, AddressOf Me.MRUComboBox_ListChanged
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Handles the ListChanged event and re-populates the items collection from the queue.
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''   mpaul   12/10/2004        Created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Private Sub MRUComboBox_ListChanged(ByVal sender As Object, ByVal e As EventArgs)
            LoadItemsFromQueue()
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Adds the controls text value to the mru list.
        ''' </summary>
        ''' <remarks>
        ''' By default, uses the controls text value.
        ''' </remarks>
        ''' <history>
        '''   mpaul   12/10/2004        Created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Overloads Sub Push()
            Me.Push(MyBase.Text.Trim)
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Adds the specified value to the mru list.
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''   mpaul   12/10/2004        Created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Overridable Overloads Sub Push(ByVal value As String)
            If value.Trim <> String.Empty AndAlso Not _mostRecentlyUsedList.Contains(value) Then
                ' push our item to the queue and broadcast the event to notify all instances their
                ' internal lists need refreshed from the queue.
                _mostRecentlyUsedList.Enqueue(value)
                RaiseEvent ListChanged(Me, EventArgs.Empty)
            End If
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Loads the items collections from the internal queue.
        ''' </summary>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''   mpaul   12/10/2004        Created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Protected Overridable Sub LoadItemsFromQueue()
            ' this is added as a pre-caution. it seems that every so often, we lose the 
            ' initial text value after this method runs.
            Dim origVal As String = MyBase.Text

            ' first thing we need to do is wipe out our most recently used collection
            ' and grab a reference to the enumerator object.
            MyBase.Items.Clear()
            Dim enumerator As IEnumerator = _mostRecentlyUsedList.GetEnumerator()

            ' next, simply enumerate our collection and add our items to our list.
            While enumerator.MoveNext()
                MyBase.Items.Insert(0, enumerator.Current, enumerator.Current)
            End While

            MyBase.Text = origVal
        End Sub

        ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' We need to discard any characters that are not numeric or control chars. 
        ''' </summary>
        ''' <param name="e"></param>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''     mpaul   12/10/2004  Created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Protected Overrides Sub OnKeyPress(ByVal e As System.Windows.Forms.KeyPressEventArgs)
            If Not Char.IsDigit(e.KeyChar) AndAlso Not Char.IsControl(e.KeyChar) AndAlso Not Char.IsWhiteSpace(e.KeyChar) Then
                e.Handled = True
            End If
        End Sub

            ''' -----------------------------------------------------------------------------
        ''' <summary>
        ''' Gets the policy number. 
        ''' </summary>
        ''' <value></value>
        ''' <remarks>
        ''' </remarks>
        ''' <history>
        '''     mpaul   12/10/2004  Created
        ''' </history>
        ''' -----------------------------------------------------------------------------
        Public Shadows ReadOnly Property Value() As Long
            Get
                Dim tryParse As Long = 0
                If MyBase.Text.Trim <> String.Empty Then
                    If IsNumeric(MyBase.Text.Trim) Then
                        Try
                            tryParse = System.Int64.Parse(MyBase.Text.Trim)
                        Catch ex As Exception
                            MyBase.Text = String.Empty
                        End Try
                    Else
                        MyBase.Text = String.Empty
                    End If
                End If
                Return tryParse
            End Get
        End Property
    End Class
End Namespace