The first bit of code will be the actual Listbox itself. First we need to decide what is going to hold the list itself and declare the necessary variables to hold it in memory.
A listbox is basically a list of values in a one dimensional array. It may be possible to extend this later to more dimensions but I will leave that for another component as that adds a lot of extra complexity.
So I need a variable for the array:
Dim PJCustList() ' Dimension a Dynamic Array to act as a container for our listbox data
You may look at this and wonder why I have not specified a size for the array
e.g.
Dim Array(1000) ' Dimensions an array of 1000 items
The reason is because we have no idea of how large our array will need to be. VBScript allows us to dimension an array without specifying a size and also allows us to redimension the array later to add or remove extra item spaces. This is ideal for what we want as listboxes generally have some way to add/remove items from the list.
Unfortunately there is a potential problem as redimensioning an array will wipe all data we have put into it. The easiest way to get around this is to dimension an identical array and copy all the data into it before we redimension the first one.
So we need :
Dim PJCustList() ' Dimension a Dynamic Array to act as a container for our listbox data
Dim PJCustListTemp() ' Dimension an identical array to copy data into
This means we now have the means to store the values. We also need to declare some other variables here. In order to make this component as reusable as possible we need an easy way to tell the script what the name of the other objects it will be working with are and to check for their existence in DesktopX.
e.g.
Const |
UpArrow |
= |
"PJLBUp", _ |
|
DownArrow |
= |
"PJLBDown", _ |
|
ScrollBar |
= |
"PJLBScrollBar", _ |
|
ScrollThumb |
= |
"PJLBScrollThumb", _ |
|
ItemPrefix |
= |
"PJLBItem" |
We also need to Dimension some other variables.
Dim |
PJCustListIndex |
' Index for our custom array |
Dim |
liststart |
' Holds the starting item in the visible list |
Dim |
listend |
' Holds the ending item in the visible list |
Dim |
itemcount |
' Count of how many item objects exist |
Dim |
PJLBOK |
' Holds True if all necessary objects exist.
' False if any are missing
' This is to aid in error detection. |
We can start coding our first procedure ( CreateLB ).
The following is the code for this:
Sub CreateLB
ReDim PJCustList(1)
ReDim PJCustListTemp(1)
' First find out how many Item Objects we have. Items must be named sequentially.
' We assume there is at least one item. Error checking for this will be added later.
found = True
currnum = 1
listcount = 0
While found = True And listcount < 1000 ' No way to enter an endless loop.
listcount = listcount+1
If DesktopX.IsObject(ItemPrefix & listcount) Then
found =True
Else
found=False
End If
Wend
itemcount = listcount-1
' Blank the items (in effect clearing the listbox)
For i = 1 To itemcount
DesktopX.ScriptObject(ItemPrefix & i).Object.Text = ""
Next
If itemcount > UBound(PJCustList) Then ' If we have more item objects than list items hide the vertical scroll bar
DesktopX.ScriptObject(ScrollThumb).Object.Visible=False
Else
DesktopX.ScriptObject(ScrollThumb).Object.Visible=True
End If
DesktopX.ScriptObject(ScrollThumb).Object.Move 0,1 ' Move the Scrollthumb to the top of the scrollbar
liststart = 0 ' Initalise the list start
End Sub
This code is enough to create an empty listbox array for use later.