home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Toolbox / Visual Basic Toolbox (P.I.E.)(1996).ISO / sorting / sortcoll / sortcoll.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1995-12-30  |  8.9 KB  |  253 lines

  1. VERSION 4.00
  2. Begin VB.Form Form1 
  3.    Caption         =   "SortedCollection Demo"
  4.    ClientHeight    =   5820
  5.    ClientLeft      =   420
  6.    ClientTop       =   1740
  7.    ClientWidth     =   9720
  8.    Height          =   6540
  9.    Icon            =   "SORTCOLL.frx":0000
  10.    Left            =   345
  11.    LinkTopic       =   "Form1"
  12.    LockControls    =   -1  'True
  13.    ScaleHeight     =   5820
  14.    ScaleWidth      =   9720
  15.    Top             =   1095
  16.    Width           =   9870
  17.    Begin VB.Frame fraErrorAction 
  18.       Caption         =   "ErrorAction property"
  19.       Height          =   1425
  20.       Left            =   180
  21.       TabIndex        =   7
  22.       Top             =   4290
  23.       Width           =   2685
  24.       Begin VB.OptionButton optErrorAction 
  25.          Caption         =   "Replace Item"
  26.          Height          =   195
  27.          Index           =   3
  28.          Left            =   150
  29.          TabIndex        =   11
  30.          Top             =   1080
  31.          Width           =   2085
  32.       End
  33.       Begin VB.OptionButton optErrorAction 
  34.          Caption         =   "Ignore Request"
  35.          Height          =   195
  36.          Index           =   2
  37.          Left            =   150
  38.          TabIndex        =   10
  39.          Top             =   810
  40.          Width           =   2085
  41.       End
  42.       Begin VB.OptionButton optErrorAction 
  43.          Caption         =   "Inform User"
  44.          Height          =   195
  45.          Index           =   1
  46.          Left            =   150
  47.          TabIndex        =   9
  48.          Top             =   540
  49.          Value           =   -1  'True
  50.          Width           =   2085
  51.       End
  52.       Begin VB.OptionButton optErrorAction 
  53.          Caption         =   "Raise Error"
  54.          Height          =   195
  55.          Index           =   0
  56.          Left            =   150
  57.          TabIndex        =   8
  58.          Top             =   270
  59.          Width           =   2085
  60.       End
  61.    End
  62.    Begin VB.ListBox List1 
  63.       Height          =   2985
  64.       Left            =   120
  65.       MultiSelect     =   2  'Extended
  66.       TabIndex        =   1
  67.       Top             =   480
  68.       Width           =   3195
  69.    End
  70.    Begin VB.CommandButton Command1 
  71.       Caption         =   "Add another image"
  72.       Height          =   555
  73.       Left            =   180
  74.       TabIndex        =   0
  75.       Top             =   3630
  76.       Width           =   2055
  77.    End
  78.    Begin VB.Label Label3 
  79.       Caption         =   "SortedCollection Keys:"
  80.       Height          =   285
  81.       Left            =   120
  82.       TabIndex        =   6
  83.       Top             =   150
  84.       Width           =   1875
  85.    End
  86.    Begin MSComDlg.CommonDialog cdlgOpen 
  87.       Left            =   2550
  88.       Top             =   3630
  89.       _version        =   65536
  90.       _extentx        =   847
  91.       _extenty        =   847
  92.       _stockprops     =   0
  93.       cancelerror     =   -1  'True
  94.       defaultext      =   "bmp"
  95.       dialogtitle     =   "Open"
  96.       filename        =   "*.bmp;*.cur;*.ico;*.wmf"
  97.       filter          =   "Images (.bmp .ico .cur .wmf)|*.bmp;*.ico;*.wmf;*.cur"
  98.    End
  99.    Begin VB.Label lblFileName 
  100.       AutoSize        =   -1  'True
  101.       Height          =   195
  102.       Left            =   4290
  103.       TabIndex        =   5
  104.       Top             =   1080
  105.       Width           =   45
  106.    End
  107.    Begin VB.Label Label2 
  108.       Alignment       =   1  'Right Justify
  109.       Caption         =   "FileName:"
  110.       Height          =   285
  111.       Left            =   3420
  112.       TabIndex        =   4
  113.       Top             =   1080
  114.       Width           =   825
  115.    End
  116.    Begin VB.Label lblSize 
  117.       Height          =   285
  118.       Left            =   4320
  119.       TabIndex        =   3
  120.       Top             =   690
  121.       Width           =   1515
  122.    End
  123.    Begin VB.Label Label1 
  124.       Alignment       =   1  'Right Justify
  125.       Caption         =   "Size:"
  126.       Height          =   285
  127.       Left            =   3420
  128.       TabIndex        =   2
  129.       Top             =   690
  130.       Width           =   825
  131.    End
  132.    Begin VB.Image imgImage 
  133.       Height          =   2025
  134.       Left            =   3450
  135.       Top             =   1470
  136.       Width           =   2505
  137.    End
  138.    Begin VB.Menu mnuFile 
  139.       Caption         =   "File"
  140.       Begin VB.Menu mnuFileAbout 
  141.          Caption         =   "About"
  142.       End
  143.       Begin VB.Menu dash 
  144.          Caption         =   "-"
  145.       End
  146.       Begin VB.Menu mnuExit 
  147.          Caption         =   "Exit"
  148.       End
  149.    End
  150.    Begin VB.Menu mnuPopup 
  151.       Caption         =   "Popup"
  152.       Visible         =   0   'False
  153.       Begin VB.Menu mnuRemove 
  154.          Caption         =   "Remove Selected Items"
  155.       End
  156.    End
  157. Attribute VB_Name = "Form1"
  158. Attribute VB_Creatable = False
  159. Attribute VB_Exposed = False
  160. Option Explicit
  161. Dim sortcollImages As New SortedCollection
  162. Private Sub Command1_Click()
  163. On Local Error Resume Next
  164. Dim NewImage As VImage
  165. Dim FullFileNameKey As String
  166. Dim Counter As Integer
  167.    cdlgOpen.ShowOpen
  168.    If Err.Number = 0 Then   'Most likely cancel was selected if this is false
  169.       FullFileNameKey = cdlgOpen.FileName
  170.       'We're going to use this full file name as our key
  171.       
  172.       'Instantiate a new VImage
  173.       Set NewImage = New VImage
  174.       
  175.       'Load up the contents of the new object
  176.       NewImage.Size = FileLen(FullFileNameKey)
  177.       NewImage.FileName = FullFileNameKey             'This property extracts the 8.3 filename
  178.       Set NewImage.Image = LoadPicture(FullFileNameKey)      'Counter'm surprised that LoadPicture works!
  179.       
  180.       'Now we can add the new object to the SortedCollection
  181.       'Remember that you *MUST* supply a key of some sort to the Add method
  182.       sortcollImages.Add NewImage, FullFileNameKey
  183.       
  184.       'To demonstrate a point more than anything, here we recreate the entire list box contents
  185.       'based solely upon the SortedCollection indexes.  Note that the .Sorted property of this list
  186.       'box is set to False.
  187.       List1.Clear
  188.       For Counter = 1 To sortcollImages.Count
  189.          List1.AddItem LCase(sortcollImages.Key(Counter))
  190.       Next Counter
  191.       '
  192.       'Note that this could have been achieved using a single line of code:
  193.       '    List1.AddItem LCase(FullFileNameKey), sortcollImages.IndexOf(FullFileNameKey) - 1
  194.       
  195.       'Clean up
  196.       Set NewImage = Nothing
  197.       
  198.       List1.ListIndex = sortcollImages.IndexOf(FullFileNameKey) - 1
  199.       List1_Click
  200.       'This will display the new item, since it will fire the List1_Click event
  201.          
  202.    End If     '(Error = 0)
  203.       
  204. End Sub
  205. Private Sub Form_Load()
  206.    sortcollImages.ErrorAction = 1       'Merely inform user
  207. End Sub
  208. Private Sub List1_Click()
  209.    'Go ahead and try this with a generic Collection!! Ha Ha.
  210.    With sortcollImages.Item(List1.ListIndex + 1)
  211.       lblSize = .Size
  212.       lblFileName = .FileName
  213.       imgImage.Picture = .Image
  214.    End With
  215. End Sub
  216. Private Sub List1_MouseDown(Button As Integer, Shift As Integer, X As Single, Y As Single)
  217.    If List1.ListIndex > -1 And Button = vbRightButton Then PopupMenu mnuPopup
  218. End Sub
  219. Private Sub mnuExit_Click()
  220.    Unload Me
  221. End Sub
  222. Private Sub mnuFileAbout_Click()
  223. Dim msg As String
  224. msg = "This example demonstrates the flexibility of the SortedCollection class. When you use the Add method " & _
  225.    "to add a new member to a SortedCollection (in this case, a simple VImage object), the member is stored in place " & _
  226.    "according to its key value (in this case, the full file name).  If the key value is not unique, then an attempted Add will cause " & _
  227.    "the private procedure HandleDuplicateIndex to fire, and your application response to the duplicate index value can be " & _
  228.    "controlled programmatically by setting the .ErrorAction property of the SortedCollection object. " & vbCrLf & vbCrLf
  229. msg = msg & "SortedCollection supports the standard collection methods: Item(), Count, Remove() and Clear.  It adds " & _
  230.    "the following new methods: Key(), KeyMixedCase(), IndexOf(), and KeyInUse()." & vbCrLf & vbCrLf & _
  231.    "Please see the source code for details concerning the Key(), IndexOf() and KeyInUse() methods of the SortedCollection class. " & _
  232.    vbCrLf & vbCrLf & "Comments, suggestions, questions and improvements should be forwarded to: " & vbCrLf & _
  233.    "Chris Vel
  234. zquez, 74073.1566@compuserve.com"
  235.    MsgBox msg, vbInformation
  236. End Sub
  237. Private Sub mnuRemove_Click()
  238. Dim Counter As Integer
  239. Dim ListMax As Integer
  240.    ListMax = List1.ListCount
  241.    For Counter = ListMax - 1 To 0 Step -1
  242.       If List1.Selected(Counter) Then
  243.          sortcollImages.Remove Counter + 1
  244.          List1.RemoveItem Counter
  245.       End If
  246.    Next Counter
  247.    If List1.ListCount > 0 Then List1.ListIndex = 0
  248. End Sub
  249. Private Sub optErrorAction_Click(Index As Integer)
  250.    'Change this to view the possible behaviors.
  251.    sortcollImages.ErrorAction = Index
  252. End Sub
  253.