Source code for Issue Number 40

Copyright 1997-2000 by C&D Programming Corp. All Rights Reserved. Source code may not be reproduced except for use in a compiled executable. All rights reserved. If you would like to reprint any or all of this code please email us at info@codeoftheweek.com

Code of the Week Home


Source Code

Just paste this source code into a class module called cComboHistory and include it in your project.

'----------------------------------------------------------------------
'
'   Class Name:     cComboHistory
'   Written By:     C&D Programming Corp.
'   Create Date:    5/98
'   Copyright:      Copyright 1998 by C&D Programming Corp.  Source
'                   code may not be reproduced except for use in a
'                   compiled executable.  All rights reserved.  If
'                   you would like to reprint any or all of this
'                   code please email us at info@codeoftheweek.com
'----------------------------------------------------------------------
Option Explicit

Public ComboBoxControl As ComboBox
Public KeyName As String
Public MaxItems As Long

Public Sub Save()
    Dim X As Long

    If ComboBoxControl Is Nothing Then
        Exit Sub
    End If
    ' don't save anything if the list is empty.
    If ComboBoxControl.ListCount = 0 Then
        Exit Sub
    End If
    ' save all the items for later retrieval.  First save the number of items
    ' and then save each item.
    SaveSetting App.Title, KeyName, "ComboCount", ComboBoxControl.ListCount - 1
    For X = 0 To ComboBoxControl.ListCount - 1
        SaveSetting App.Title, KeyName, "Combo" & X, ComboBoxControl.List(X)
    Next
End Sub

Public Sub Load()
    Dim X As Long

    If ComboBoxControl Is Nothing Then
        Exit Sub
    End If
    ' Don't do anything if there are no saved items.
    X = GetSetting(App.Title, KeyName, "ComboCount", -1)
    If X = -1 Then
        Exit Sub
    End If
    For X = 0 To GetSetting(App.Title, KeyName, "ComboCount", 0)
        ComboBoxControl.AddItem GetSetting(App.Title, KeyName, "Combo" & X, "")
    Next
End Sub

Public Sub AddToList(sData As String)
    Dim X As Long

    ' let's not add a duplicate search string...
    If ComboBoxControl.List(0) = sData Then
        Exit Sub
    End If
    ' add item to list and remove the bottom item if it is greater than
    ' the max number of items to store in the list.
    ComboBoxControl.AddItem sData, 0
    If ComboBoxControl.ListCount > MaxItems Then
        For X = ComboBoxControl.ListCount To MaxItems + 1 Step -1
            ComboBoxControl.RemoveItem X - 1
        Next
    End If
End Sub

Private Sub Class_Initialize()
    MaxItems = 10   ' default number of history items to maintain
End Sub