home *** CD-ROM | disk | FTP | other *** search
/ Planet Source Code Jumbo …e CD Visual Basic 1 to 7 / 1_2002.ISO / Data / Zips / CODE_UPLOAD90398172000.psc / ListBox.ctl (.txt) next >
Encoding:
Visual Basic Form  |  2000-08-17  |  25.2 KB  |  689 lines

  1. VERSION 5.00
  2. Begin VB.UserControl CustomListBox 
  3.    Appearance      =   0  'Flat
  4.    BackColor       =   &H00000000&
  5.    BorderStyle     =   1  'Fixed Single
  6.    ClientHeight    =   1575
  7.    ClientLeft      =   0
  8.    ClientTop       =   0
  9.    ClientWidth     =   3450
  10.    ScaleHeight     =   105
  11.    ScaleMode       =   3  'Pixel
  12.    ScaleWidth      =   230
  13.    Begin VB.PictureBox ImageSizer 
  14.       AutoSize        =   -1  'True
  15.       BorderStyle     =   0  'None
  16.       Height          =   495
  17.       Left            =   1920
  18.       ScaleHeight     =   33
  19.       ScaleMode       =   3  'Pixel
  20.       ScaleWidth      =   81
  21.       TabIndex        =   2
  22.       Top             =   960
  23.       Visible         =   0   'False
  24.       Width           =   1215
  25.    End
  26.    Begin VB.PictureBox TheList 
  27.       Appearance      =   0  'Flat
  28.       AutoRedraw      =   -1  'True
  29.       BackColor       =   &H00FFFFFF&
  30.       BorderStyle     =   0  'None
  31.       ForeColor       =   &H80000008&
  32.       Height          =   1185
  33.       Left            =   60
  34.       ScaleHeight     =   79
  35.       ScaleMode       =   3  'Pixel
  36.       ScaleWidth      =   188
  37.       TabIndex        =   1
  38.       Top             =   60
  39.       Width           =   2820
  40.    End
  41.    Begin VB.PictureBox ScrollBox 
  42.       Appearance      =   0  'Flat
  43.       BackColor       =   &H80000005&
  44.       BorderStyle     =   0  'None
  45.       ForeColor       =   &H80000008&
  46.       Height          =   225
  47.       Left            =   3285
  48.       Picture         =   "ListBox.ctx":0000
  49.       ScaleHeight     =   15
  50.       ScaleMode       =   3  'Pixel
  51.       ScaleWidth      =   10
  52.       TabIndex        =   0
  53.       Top             =   15
  54.       Width           =   150
  55.    End
  56.    Begin VB.Shape VScrollBar 
  57.       BackColor       =   &H00C0C0C0&
  58.       BackStyle       =   1  'Opaque
  59.       BorderColor     =   &H00808080&
  60.       Height          =   1575
  61.       Left            =   3240
  62.       Top             =   0
  63.       Width           =   180
  64.    End
  65. Attribute VB_Name = "CustomListBox"
  66. Attribute VB_GlobalNameSpace = False
  67. Attribute VB_Creatable = True
  68. Attribute VB_PredeclaredId = False
  69. Attribute VB_Exposed = True
  70. ' Custom Listbox Control 1.1
  71. ' By Patrick Gillespie (patorjk@aol.com)
  72. ' 8.16.00
  73. ' http://www.patorjk.com/
  74. ' This is an example on how to create your own listbox. This listbox has most of the
  75. ' features of a normal listbox, except you can also use picture backgrounds in it.
  76. ' This example is still being improved on, so if you find any errors or have any
  77. ' suggestions please email me.
  78. Option Explicit
  79. Dim ListItems() As String
  80. Dim ListCount As Integer
  81. Dim SortList As Boolean
  82. Dim SelItem As Integer
  83. Dim OldSelItem As Integer
  84. Dim ListItemHeight As Long
  85. Dim SelColor As Long
  86. Dim CanScroll As Boolean
  87. ' for scrollbar
  88. Dim OffSetY As Integer
  89. Dim TopY As Integer
  90. ' picture info
  91. Dim ThePic As Picture, IsGraphical As Boolean
  92. Dim PicWidth As Integer, PicHeight As Integer
  93. Dim PicArray() As StdPicture, PicCount As Integer
  94. Dim PicIndex() As Integer
  95. Private WithEvents TheFont As StdFont
  96. Attribute TheFont.VB_VarHelpID = -1
  97. ' The events
  98. Public Event Click()
  99. Public Event DblClick()
  100. Public Event KeyDown(KeyCode As Integer, Shift As Integer)
  101. Public Event KeyPress(KeyAscii As Integer)
  102. Public Event KeyUp(KeyCode As Integer, Shift As Integer)
  103. Public Event MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  104. Public Event MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  105. Public Event MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  106. Public Property Get Graphical() As OLE_OPTEXCLUSIVE
  107.     ' This is called when we want to know the state of the Graphical property
  108.     Graphical = IsGraphical
  109. End Property
  110. Public Property Let Graphical(ByVal TheOpinion As OLE_OPTEXCLUSIVE)
  111.     ' This sets the Graphical property
  112.     IsGraphical = TheOpinion
  113.     Call DrawListBox
  114.     PropertyChanged "Graphical"
  115. End Property
  116. Public Property Get Picture() As Picture
  117.     ' This is when we want to know the picture being stored
  118.     Set Picture = ThePic
  119. End Property
  120. Public Property Set Picture(ByVal LaPic As Picture)
  121.     ' This sets the Picture property
  122.     Set ThePic = LaPic
  123.     Call DrawListBox
  124.     PropertyChanged "Picture"
  125. End Property
  126. Public Property Get FontInfo() As StdFont
  127.     ' Get the font information
  128.     Set FontInfo = TheFont
  129. End Property
  130. Public Property Set FontInfo(NewFont As StdFont)
  131.     ' Set the new font information and then redraw
  132.     Set TheFont = NewFont
  133.     Set TheList.Font = NewFont
  134.     Call DrawListBox
  135.     PropertyChanged "FontInfo"
  136. End Property
  137. Public Property Get SelBoxColor() As OLE_COLOR
  138.     ' Gets current color
  139.     SelBoxColor = SelColor
  140. End Property
  141. Public Property Let SelBoxColor(ByVal NewColor As OLE_COLOR)
  142.     ' Sets color
  143.     SelColor = NewColor
  144.     ' redraw list
  145.     DrawListBox
  146.     PropertyChanged "SelBoxColor"
  147. End Property
  148. Public Property Get ForeColor() As OLE_COLOR
  149.     ' Gets current color
  150.     ForeColor = TheList.ForeColor
  151. End Property
  152. Public Property Let ForeColor(ByVal NewColor As OLE_COLOR)
  153.     ' Sets color
  154.     TheList.ForeColor = NewColor
  155.     ' redraw list with color
  156.     DrawListBox
  157.     PropertyChanged "ForeColor"
  158. End Property
  159. Public Property Get ScrollBarBorderColor() As OLE_COLOR
  160.     ' Gets current color
  161.     ScrollBarBorderColor = VScrollBar.BorderColor
  162. End Property
  163. Public Property Let ScrollBarBorderColor(ByVal NewColor As OLE_COLOR)
  164.     ' Sets color
  165.     VScrollBar.BorderColor = NewColor
  166.     PropertyChanged "ScrollBarBorderColor"
  167. End Property
  168. Public Property Get ScrollBarBackColor() As OLE_COLOR
  169.     ' Gets current color
  170.     ScrollBarBackColor = VScrollBar.BackColor
  171. End Property
  172. Public Property Let ScrollBarBackColor(ByVal NewColor As OLE_COLOR)
  173.     ' Sets color
  174.     VScrollBar.BackColor = NewColor
  175.     PropertyChanged "ScrollBarBackColor"
  176. End Property
  177. Public Property Get BackColor() As OLE_COLOR
  178.     ' Gets current color
  179.     BackColor = TheList.BackColor
  180. End Property
  181. Public Property Let BackColor(ByVal NewColor As OLE_COLOR)
  182.     ' Sets color
  183.     TheList.BackColor = NewColor
  184.     PropertyChanged "BackColor"
  185. End Property
  186. Public Property Get Sorted() As OLE_OPTEXCLUSIVE
  187.     ' This is called when we want to know the state of the Graphical property
  188.     Sorted = SortList
  189. End Property
  190. Public Property Let Sorted(ByVal TheOpinion As OLE_OPTEXCLUSIVE)
  191.     ' This sets the Graphical property
  192.     SortList = TheOpinion
  193.     PropertyChanged "Sorted"
  194. End Property
  195. Private Sub ScrollBox_KeyDown(KeyCode As Integer, Shift As Integer)
  196.     Call Scrolllistbasedonkey(KeyCode)
  197. End Sub
  198. Private Sub ScrollBox_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  199.     ' Set up variables in case the list needs to be scrolled (see mouse move event)
  200.     Dim CurPos As POINTAPI
  201.     Call GetCursorPos(CurPos)
  202.     OffSetY = CurPos.y
  203.     TopY = ScrollBox.Top
  204. End Sub
  205. Private Sub ScrollBox_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  206.     ' Move the list depending on the location of the scrollbox
  207.     Dim YPos As Integer, CurPos As POINTAPI
  208.     If Button = 1 And CanScroll = True Then
  209.         Call GetCursorPos(CurPos)
  210.         YPos = TopY - (OffSetY - CurPos.y)
  211.         If YPos < 1 Then
  212.             ScrollBox.Top = 1
  213.             Call ScrollList(ScrollBox.Top)
  214.         ElseIf YPos > (VScrollBar.Height - ScrollBox.ScaleHeight - 1) Then
  215.             ScrollBox.Top = VScrollBar.Height - ScrollBox.Height - 1
  216.             Call ScrollList(ScrollBox.Top)
  217.         Else
  218.             ScrollBox.Top = YPos
  219.             Call ScrollList(ScrollBox.Top)
  220.         End If
  221.     ElseIf CanScroll = False Then
  222.         ScrollBox.Top = 1
  223.     End If
  224. End Sub
  225. Private Sub TheList_Click()
  226.     ' Raise the Click event
  227.     RaiseEvent Click
  228. End Sub
  229. Private Sub TheList_DblClick()
  230.     ' Raise the DblClick event
  231.     RaiseEvent DblClick
  232. End Sub
  233. Private Sub TheList_KeyDown(KeyCode As Integer, Shift As Integer)
  234.     Call Scrolllistbasedonkey(KeyCode)
  235.     RaiseEvent KeyDown(KeyCode, Shift)
  236. End Sub
  237. Private Sub TheList_KeyPress(KeyAscii As Integer)
  238.     RaiseEvent KeyPress(KeyAscii)
  239. End Sub
  240. Private Sub TheList_KeyUp(KeyCode As Integer, Shift As Integer)
  241.     RaiseEvent KeyUp(KeyCode, Shift)
  242. End Sub
  243. Private Sub TheList_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  244.     Dim YPos As Integer
  245.     Call SetUpList
  246.     YPos = CInt(y)
  247.     SelItem = Int(YPos / ListItemHeight)
  248.     Call DrawSelItem
  249.     RaiseEvent MouseDown(Button, Shift, x, y)
  250. End Sub
  251. Private Sub TheList_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  252.     ' This code is excuted if the user is holding the left mouse button down and
  253.     ' moving the mouse.
  254.     Dim CurPos As POINTAPI, ListWndSize As RECT
  255.     If Button = 1 Then
  256.         Dim YPos As Integer
  257.         Call SetUpList
  258.         Call GetWindowRect(UserControl.hwnd, ListWndSize)
  259.         Call GetCursorPos(CurPos)
  260.         
  261.         If CurPos.y < ListWndSize.Top Then
  262.             ' The cursor is above the listbox
  263.             Call Timeout(0.1)
  264.             'Call MoveUp
  265.         ElseIf CurPos.y > ListWndSize.Bottom Then
  266.             ' The cursor is below the listbox
  267.             Call Timeout(0.1)
  268.             'Call MoveDown
  269.         Else
  270.             ' The cursor is on the listbox so just select a new item if
  271.             ' one is moved over.
  272.             YPos = CInt(y)
  273.             SelItem = Int(YPos / ListItemHeight)
  274.         End If
  275.         
  276.         Call DrawSelItem
  277.     End If
  278.     RaiseEvent MouseMove(Button, Shift, x, y)
  279. End Sub
  280. Private Sub DrawSelItem()
  281.     ' This sub draws the selected item box around the selected item
  282.     Dim Y1 As Long, Y2 As Long
  283.     If SelItem <= ListCount And SelItem >= 0 And SelItem <> OldSelItem Then
  284.         ' Draw Selected Box
  285.         Y1 = CLng(SelItem * ListItemHeight)
  286.         Y2 = CLng((SelItem + 1) * ListItemHeight)
  287.         Call DrawRectangle(TheList.hdc, 0, Y1, TheList.ScaleWidth, Y2, SelColor)
  288.         Call UpdateItem(SelItem)
  289.         ' Clear Away Old Select Box
  290.         If OldSelItem <> -1 Then
  291.             Y1 = CLng(OldSelItem * ListItemHeight)
  292.             Y2 = CLng((OldSelItem + 1) * ListItemHeight)
  293.             If IsGraphical = True Then
  294.                 ' if a graphic is being used then clear away the old select box with
  295.                 ' the graphic image that goes in it's place
  296.                 Call DrawImageRect(0, Y1, TheList.ScaleWidth, Y2)
  297.             Else
  298.                 Call DrawRectangle(TheList.hdc, 0, Y1, TheList.ScaleWidth, Y2, TheList.BackColor)
  299.             End If
  300.             Call UpdateItem(OldSelItem)
  301.         End If
  302.         OldSelItem = SelItem
  303.     End If
  304. End Sub
  305. Private Sub DrawImageRect(X1 As Long, Y1 As Long, X2 As Long, Y2 As Long)
  306.     ' This sub draws what the image under a selected item would look like
  307.     Dim PicsDownY1 As Integer, PicsDownY2 As Integer, Offset As Long
  308.     Dim StartCut As Long, StopCut As Long, i As Integer, i2 As Integer
  309.     PicsDownY1 = Int(Y1 / PicHeight)
  310.     PicsDownY2 = Int(Y2 / PicHeight)
  311.     ' Set up canvas to draw on
  312.     ImageSizer.Cls
  313.     ImageSizer.AutoRedraw = True
  314.     ImageSizer.Width = TheList.ScaleWidth
  315.     ImageSizer.Height = Y2 - Y1
  316.     StartCut = Y1 - (PicsDownY1 * PicHeight)
  317.     If PicsDownY2 = PicsDownY1 Then
  318.         StopCut = Y2 - (PicsDownY2 * PicHeight)
  319.         ' Draw Top - No middle or bottom area needed
  320.         For i = 0 To X2 Step PicWidth
  321.             ImageSizer.PaintPicture ThePic, i, 0, (i + 1) * PicWidth, StopCut - StartCut, 0, StartCut, (i + 1) * PicWidth, StopCut - StartCut
  322.         Next
  323.         ImageSizer.Picture = ImageSizer.Image
  324.         ImageSizer.AutoRedraw = False
  325.     Else
  326.         ' DrawTop
  327.         StopCut = PicHeight
  328.         For i = 0 To X2 Step PicWidth
  329.             ImageSizer.PaintPicture ThePic, i, 0, (i + 1) * PicWidth, StopCut - StartCut, 0, StartCut, (i + 1) * PicWidth, StopCut - StartCut
  330.         Next
  331.         ' Draw Middle
  332.         Offset = StopCut - StartCut
  333.         For i = 0 To (PicsDownY2 - PicsDownY1) - 2
  334.             For i2 = 0 To X2 Step PicWidth
  335.                 ImageSizer.PaintPicture ThePic, i2, Offset + (i * PicHeight), PicWidth, PicHeight, 0, 0, PicWidth, PicHeight
  336.             Next
  337.         Next
  338.         ' Draw Bottom
  339.         StartCut = 0
  340.         StopCut = Y2 - (PicsDownY2 * PicHeight)
  341.         For i = 0 To X2 Step PicWidth
  342.             ImageSizer.PaintPicture ThePic, i, Offset + ((PicsDownY2 - PicsDownY1) - 1) * PicHeight, i + PicWidth, StopCut - StartCut, 0, StartCut, i + PicWidth, StopCut - StartCut
  343.         Next
  344.         ImageSizer.Picture = ImageSizer.Image
  345.         ImageSizer.AutoRedraw = False
  346.     End If
  347.     TheList.PaintPicture ImageSizer.Picture, 0, Y1
  348.     ImageSizer.Picture = LoadPicture("")
  349. End Sub
  350. Private Sub TheList_MouseUp(Button As Integer, Shift As Integer, x As Single, y As Single)
  351.     RaiseEvent MouseUp(Button, Shift, x, y)
  352. End Sub
  353. Private Sub UserControl_Initialize()
  354.     Set TheFont = New StdFont
  355.     TheList.Top = 0
  356.     TheFont.Name = "Verdana"
  357.     TheFont.Size = 7
  358.     ListCount = -1
  359.     PicCount = -1
  360.     SelItem = -1
  361.     OldSelItem = -1
  362.     SetUpList
  363.     UserControl_Resize
  364. End Sub
  365. Private Sub UserControl_InitProperties()
  366.     ' This is called only when the control is first created.
  367.     ' It's not in the initialize event because at that point in time the control
  368.     ' has not yet been placed on the form.
  369.     Set FontInfo = TheFont
  370.     SelColor = &HC0C000
  371. End Sub
  372. Private Sub UserControl_KeyDown(KeyCode As Integer, Shift As Integer)
  373.     Call Scrolllistbasedonkey(KeyCode)
  374. End Sub
  375. Private Sub UserControl_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  376.     Dim XPos As Integer, YPos As Integer
  377.     XPos = CInt(x)
  378.     YPos = CInt(y)
  379.     If XPos > TheList.ScaleWidth Then
  380.         If CanScroll = True Then
  381.             If YPos < 1 Then
  382.                 ScrollBox.Top = 1
  383.                 Call ScrollList(ScrollBox.Top)
  384.             ElseIf YPos > (VScrollBar.Height - ScrollBox.ScaleHeight - 1) Then
  385.                 ScrollBox.Top = VScrollBar.Height - ScrollBox.Height - 1
  386.                 Call ScrollList(ScrollBox.Top)
  387.             Else
  388.                 ScrollBox.Top = YPos
  389.                 Call ScrollList(ScrollBox.Top)
  390.             End If
  391.         ElseIf CanScroll = False Then
  392.             ScrollBox.Top = 1
  393.         End If
  394.     End If
  395. End Sub
  396. Private Sub UserControl_ReadProperties(PropBag As PropertyBag)
  397.     ' This event is called every time the control is created except for the
  398.     ' first time you put it on the form.
  399.     TheList.BackColor = PropBag.ReadProperty("BackColor", RGB(255, 255, 255))
  400.     TheList.ForeColor = PropBag.ReadProperty("ForeColor", 0)
  401.     VScrollBar.BackColor = PropBag.ReadProperty("ScrollBarBackColor", &HC0C0C0)
  402.     VScrollBar.BorderColor = PropBag.ReadProperty("ScrollBarBorderColor", &H808080)
  403.     SelBoxColor = PropBag.ReadProperty("SelBoxColor", &HC0C000)
  404.     SortList = PropBag.ReadProperty("Sorted", False)
  405.     IsGraphical = PropBag.ReadProperty("Graphical", True)
  406.     Set ThePic = PropBag.ReadProperty("Picture", LoadPicture(""))
  407.     Set FontInfo = PropBag.ReadProperty("FontInfo", UserControl.Font)
  408.     If ThePic = UserControl.Picture Then
  409.         IsGraphical = False
  410.     End If
  411. End Sub
  412. Private Sub UserControl_Resize()
  413.     ' Set things up right, and redraw the list if needed
  414.     Static OldWidth As Long, OldHeight As Long
  415.     UserControl.ScaleMode = 3
  416.     If UserControl.ScaleWidth > 50 Then
  417.         TheList.Width = UserControl.ScaleWidth - VScrollBar.Width - 1
  418.         TheList.Left = 0
  419.         VScrollBar.Left = TheList.Width + 1
  420.         ScrollBox.Left = VScrollBar.Left + 1
  421.     End If
  422.     If UserControl.ScaleHeight > 50 Then
  423.         VScrollBar.Height = UserControl.ScaleHeight
  424.     End If
  425.     Call SetUpList
  426.     If UserControl.ScaleWidth > OldWidth And OldWidth <> 0 Then
  427.         Call DrawListBox
  428.     Else
  429.         If UserControl.ScaleHeight > OldHeight And OldHeight <> 0 Then
  430.             Call DrawListBox
  431.         End If
  432.     End If
  433.     OldWidth = UserControl.ScaleWidth
  434.     OldHeight = UserControl.ScaleHeight
  435. End Sub
  436. Public Sub AddItem(NewItem As String, Optional PictureIndex As Integer = -1)
  437.     ' Adds an item to the list array and then redraws the list
  438.     ListCount = ListCount + 1
  439.     ReDim Preserve ListItems(ListCount) As String
  440.     ListItems(ListCount) = NewItem
  441.     ReDim Preserve PicIndex(ListCount) As Integer
  442.     If PictureIndex > PicCount Then
  443.         ' they put in an invalid pictureindex
  444.         PicIndex(ListCount) = -1
  445.     Else
  446.         PicIndex(ListCount) = PictureIndex
  447.     End If
  448.     If SortList = True Then
  449.         Call SortTheList
  450.     Else
  451.         Call DrawListBox
  452.     End If
  453.     Call ScrollScrollBox(TheList.Top * -1)
  454. End Sub
  455. Public Sub AddImage(LaPic As Picture)
  456.     PicCount = PicCount + 1
  457.     ReDim Preserve PicArray(PicCount) As StdPicture
  458.     Set PicArray(PicCount) = LaPic
  459. End Sub
  460. Public Sub SortTheList()
  461.     ' This sub sorts the list a to z
  462.     Dim i As Integer, i2 As Integer, Hold As String
  463.     For i = 0 To ListCount
  464.         For i2 = 0 To ListCount
  465.             If i <> i2 Then
  466.                 If LCase$(ListItems(i)) < LCase$(ListItems(i2)) Then
  467.                     Hold = ListItems(i)
  468.                     ListItems(i) = ListItems(i2)
  469.                     ListItems(i2) = Hold
  470.                 End If
  471.             End If
  472.         Next
  473.     Next
  474.     Call DrawListBox
  475. End Sub
  476. Private Sub DrawListBox()
  477.     ' Draw the list box
  478.     Dim i As Integer, SidePicHeight As Integer, SidePicWidth As Integer
  479.     Call SetUpList
  480.     ' clear and draw items on list
  481.     TheList.Cls
  482.     ' draw image background if set to
  483.     If IsGraphical = True Then
  484.         Call DrawImageBG
  485.     End If
  486.     ' Note: List items are drawn in two places, this sub, and in the updateitem sub
  487.     For i = 0 To ListCount
  488.         If PicIndex(i) = -1 Then
  489.             ' Item doesn't have a picture next to it
  490.             TheList.CurrentX = 3
  491.             TheList.CurrentY = (ListItemHeight * (i))
  492.             TheList.Print ListItems(i)
  493.         Else
  494.             ' Item does have a picture next to it
  495.             TheList.CurrentX = 3
  496.             TheList.CurrentY = (ListItemHeight * (i))
  497.             SidePicHeight = ListItemHeight - 2
  498.             SidePicWidth = ListItemHeight - 2
  499.             TheList.PaintPicture PicArray(PicIndex(i)), TheList.CurrentX, TheList.CurrentY + 1, SidePicWidth, SidePicHeight, 0, 0
  500.             TheList.CurrentX = 3 + SidePicWidth + 3
  501.             TheList.CurrentY = (ListItemHeight * (i))
  502.             TheList.Print ListItems(i)
  503.         End If
  504.     Next
  505.     OldSelItem = -1
  506.     DrawSelItem
  507. End Sub
  508. Private Sub DrawImageBG()
  509.     ' This sub draws the background
  510.     Dim x As Integer, y As Integer
  511.     On Error Resume Next
  512.     Call GetImageSize
  513.     For x = 0 To TheList.ScaleWidth Step PicWidth
  514.         For y = 0 To TheList.ScaleHeight Step PicHeight
  515.             TheList.PaintPicture ThePic, x, y
  516.         Next
  517.     Next
  518. End Sub
  519. Private Sub GetImageSize()
  520.     ' This sub gets the size of the image to use for the back of the listbox
  521.     Set ImageSizer.Picture = ThePic
  522.     PicWidth = ImageSizer.ScaleWidth
  523.     PicHeight = ImageSizer.ScaleHeight
  524.     ImageSizer.Picture = LoadPicture("")
  525. End Sub
  526. Private Sub SetUpList()
  527.     ' set the listbox up
  528.     ListItemHeight = CInt(TheList.TextHeight("M") + 2)
  529.     ' set list height
  530.     If (ListItemHeight * (ListCount + 1)) > UserControl.ScaleHeight - 1 Then
  531.         TheList.Height = ListItemHeight * (ListCount + 1) + 1
  532.         CanScroll = True
  533.     Else
  534.         TheList.Height = UserControl.Height
  535.         CanScroll = False
  536.     End If
  537. End Sub
  538. Private Sub UpdateItem(index As Integer, Optional ClearItem As Boolean = False)
  539.     Dim SidePicHeight As Integer, SidePicWidth As Integer
  540.     Call SetUpList
  541.     If ClearItem = True Then
  542.         TheList.Line (0, OldSelItem * ListItemHeight)-(TheList.ScaleWidth, (OldSelItem + 1) * ListItemHeight), TheList.BackColor, BF
  543.     End If
  544.     If PicIndex(index) = -1 Then
  545.         ' Item doesn't have a picture next to it
  546.         TheList.CurrentX = 3
  547.         TheList.CurrentY = (ListItemHeight * (index))
  548.         TheList.Print ListItems(index)
  549.     Else
  550.         ' Item does have a picture next to it
  551.         TheList.CurrentX = 3
  552.         TheList.CurrentY = (ListItemHeight * (index))
  553.         SidePicHeight = ListItemHeight - 2
  554.         SidePicWidth = ListItemHeight - 2
  555.         TheList.PaintPicture PicArray(PicIndex(index)), TheList.CurrentX, TheList.CurrentY + 1, SidePicWidth, SidePicHeight, 0, 0
  556.         TheList.CurrentX = 3 + SidePicWidth + 3
  557.         TheList.CurrentY = (ListItemHeight * (index))
  558.         TheList.Print ListItems(index)
  559.     End If
  560. End Sub
  561. Private Sub ScrollList(Pos As Long)
  562.     ' This sub scrolls the list
  563.     Dim SpaceToScroll As Long, TheStep As Double, BarLength As Integer
  564.     Pos = Pos - 1
  565.     BarLength = VScrollBar.Height - ScrollBox.ScaleHeight
  566.     SpaceToScroll = (ListItemHeight * (ListCount + 1)) - (UserControl.ScaleHeight - 1)
  567.     TheStep = SpaceToScroll / BarLength
  568.     TheList.Top = 0 - (Pos * TheStep)
  569. End Sub
  570. Private Sub ScrollScrollBox(ListPos As Long)
  571.     ' This sub moves the scrollbox depending on what position you want to
  572.     ' move the list to.
  573.     Dim SpaceToScroll As Long, TheStep As Double, BarLength As Integer
  574.     Dim ScrollBoxTop As Integer
  575.     BarLength = VScrollBar.Height - ScrollBox.ScaleHeight - 1
  576.     SpaceToScroll = (ListItemHeight * (ListCount + 1)) - (UserControl.ScaleHeight - 1)
  577.     TheStep = SpaceToScroll / BarLength
  578.     ScrollBoxTop = (ListPos / TheStep) + 1
  579.     If SelItem = ListCount And CanScroll = True Then
  580.         ScrollBox.Top = VScrollBar.Height - ScrollBox.ScaleHeight - 1
  581.     Else
  582.         ScrollBox.Top = ScrollBoxTop
  583.     End If
  584.     Call ScrollList(ScrollBox.Top)
  585.     ' TheList.Top = 0 - ListPos
  586. End Sub
  587. Private Sub Scrolllistbasedonkey(KeyCode As Integer)
  588.     ' This sub is called when the user presses a key on the listbox
  589.     If KeyCode = vbKeyUp Then
  590.         MoveUp
  591.     ElseIf KeyCode = vbKeyDown Then
  592.         MoveDown
  593.     End If
  594. End Sub
  595. Public Function LCount() As Integer
  596.     ' Returns the number of items in the list
  597.     LCount = ListCount + 1
  598. End Function
  599. Public Function Listindex() As Integer
  600.     ' Returns the selected item
  601.     Listindex = SelItem
  602. End Function
  603. Public Function List(index As Integer) As String
  604.     ' Returns an item in the list array depending on the index you put in
  605.     List = ListItems(index)
  606. End Function
  607. Public Sub RemoveItem(index As Integer)
  608.     ' This sub removes an item from a listbox
  609.     Dim i As Integer
  610.     ' Exit sub if item is not on the list
  611.     If index = -1 Or index > ListCount Then Exit Sub
  612.     ' Set selitem status
  613.     If index = SelItem Then
  614.         SelItem = -1
  615.         OldSelItem = -1
  616.     ElseIf SelItem > index Then
  617.         SelItem = SelItem - 1
  618.         OldSelItem = OldSelItem - 1
  619.     End If
  620.     ListCount = ListCount - 1
  621.     For i = index To ListCount
  622.         ListItems(i) = ListItems(i + 1)
  623.         PicIndex(i) = PicIndex(i + 1)
  624.     Next
  625.     If ListCount <> -1 Then
  626.         ReDim Preserve ListItems(ListCount) As String
  627.         ReDim Preserve PicIndex(ListCount) As Integer
  628.     End If
  629.     Call SetUpList
  630.     If CanScroll = True Then
  631.         Call ScrollList(ScrollBox.Top)
  632.     Else
  633.         ScrollBox.Top = 1
  634.         TheList.Top = 0
  635.     End If
  636.     Call DrawListBox
  637. End Sub
  638. Public Sub Clear()
  639.     ' Clears all the items out of the list box
  640.     ListCount = -1
  641.     ReDim ListItems(0) As String
  642.     ReDim PicIndex(0) As Integer
  643.     Call SetUpList
  644.     If CanScroll = True Then
  645.         Call ScrollList(ScrollBox.Top)
  646.     Else
  647.         ScrollBox.Top = 1
  648.         TheList.Top = 0
  649.     End If
  650.     Call DrawListBox
  651. End Sub
  652. Public Sub MoveUp()
  653.     ' Moves the selected item up one
  654.     Dim ItemTop As Long
  655.     If SelItem = -1 Or SelItem = 0 Then Exit Sub
  656.     ' Move the selected item index up one
  657.     SelItem = SelItem - 1
  658.     ItemTop = (SelItem) * ListItemHeight
  659.     If ItemTop < Abs(TheList.Top) Then
  660.         Call ScrollScrollBox(ItemTop)
  661.     End If
  662.     Call DrawSelItem
  663. End Sub
  664. Public Sub MoveDown()
  665.     ' Moves the selected item down one
  666.     Dim ItemBottom As Long
  667.     If SelItem = -1 Or SelItem = ListCount Then Exit Sub
  668.     ' Move the selected item index up one
  669.     SelItem = SelItem + 1
  670.     ItemBottom = (SelItem + 1) * ListItemHeight
  671.     If ItemBottom > Abs(TheList.Top) + UserControl.ScaleHeight Then
  672.         Call ScrollScrollBox(ItemBottom - UserControl.ScaleHeight)
  673.     End If
  674.     Call DrawSelItem
  675. End Sub
  676. Private Sub UserControl_WriteProperties(PropBag As PropertyBag)
  677.     ' This event is called when the control needs to save the values of the
  678.     ' properties (like before you go into run mode).
  679.     PropBag.WriteProperty "BackColor", TheList.BackColor
  680.     PropBag.WriteProperty "FontInfo", FontInfo
  681.     PropBag.WriteProperty "ForeColor", TheList.ForeColor
  682.     PropBag.WriteProperty "Graphical", IsGraphical
  683.     PropBag.WriteProperty "Picture", ThePic
  684.     PropBag.WriteProperty "ScrollBarBackColor", VScrollBar.BackColor
  685.     PropBag.WriteProperty "ScrollBarBorderColor", VScrollBar.BorderColor
  686.     PropBag.WriteProperty "SelBoxColor", SelColor
  687.     PropBag.WriteProperty "Sorted", SortList
  688. End Sub
  689.