"Thomas Beyerlein" <tb***@yahoo.com> wrote in message news:OU**************@TK2MSFTNGP09.phx.gbl...
I am writing code for a list box editor, one of the lists is large was
hoping that there is a way to either speed it up by making the server do
the IF statements of there is a faster way of checking the lists? Any
help would be useful. Have posted the code I am using below
Tom
parentControl = New Windows.Forms.ListBox
parentControl = control
table = sourceTable
column = field
' populate right list box
For i = 0 To control.Items.Count - 1 Step 1
lstDestination.Items.Add(control.Items.Item(i))
Next
' populate left list box
tempString = "SELECT " & field & " FROM " & sourceTable
myDataSet = Q.Query(tempString)
For i = 0 To myDataSet.Tables("myData").Rows.Count - 1 Step 1
' add to box only if it does not have that item already
If
lstDestination.Items.Contains(myDataSet.Tables("my Data").Rows(i).Item(fi
eld)) = False Then
lstSource.Items.Add(myDataSet.Tables("myData").Row s(i).Item(field))
End If
Next
You should first reduce the amount of dereferencing going on. The With
statement exists for several reasons. It not only simplifies your coding
(making it easier to read and write) it also dereferences objects. That's
a huge overhead when dealing with the exact same object repeatedly,
as you are.
Note that only one object can be dereferenced with the With statement.
If you need multiple dereferences (as you do), use local variables as well.
The With statement actually provides an implicit local variable and they
can be nested if necessary (as I've done below).
You should also use BeginUpdate and EndUpdate on the lists during
an update. There's no point drawing the list each time an item is added.
Just add the items in a oner, then refresh the list.
All in all, the following amendments should significantly improve
performance.
' These first few statements don't do anything useful. Declare them
' immediately before you use them--don't place all your declarations
' at the top of a sub or function unless they're guaranteed to be used
' before an Exit Sub or Exit Function is encountered.
parentControl = New Windows.Forms.ListBox
parentControl = control
table = sourceTable
column = field
' Dereference the destination listbox items collections (explicit variable).
Dim dstItems as ListBox.ObjectCollection = lstDestination.Items
' Dereference the destination list (implicit variable)
With lstDestination
' stop drawing control
.BeginUpdate()
' Dereference control items (implicit variable).
With control.Items
' Update the destination list
For i = 0 To .Count - 1 Step 1
dstItems.Add(.Item(i))
Next
End With
' refresh control
.EndUpdate()
End With
tempString = "SELECT " & field & " FROM " & sourceTable
myDataSet = Q.Query(tempString)
' Dereference the source listbox items collections (explicit variable).
Dim srcItems as ListBox.ObjectCollection = lstSource.Items
' Dereference the source list (implicit variable).
With lstSource
' Stop drawing the list
.BeginUpdate()
' Dereference the data table (implicit variable)
With myDataSet.Tables("myData")
' Add items not in destination list to source list.
For i = 0 To .Rows.Count - 1 Step 1
If dstItems.Contains(.Rows(i).Item(field)) = False Then
srcItems.Add(.Rows(i).Item(field))
End If
Next
End With
' Refresh the list
.EndUpdate()
End With