How to store growiing list box values programatically

C

CS Hayes

I need to store a list box's source in code (or a hidden table? I don't
know.) The list grows. The user can add an item via form to the list.

I have a form and on the add click I want to add a 5 column entry to the
listbox

I know of the "additem" key word but how do I add the 5 items to one row

My items are like this: start (string), finish (string), difference
(integer), multiplier (double), total (double)
 
C

CS Hayes

Ok, how about a growing array
I'm keeping the code to the form right now

I have a list box with 5 columns. 1) start (string), 2) finish (string), 3)
difference
(integer), 4) multiplier (double), 5) total (double)

These are all calculated in a Private sub on the on click event from the form.

If I declare a variable for the listbox row source [.List () =
strLstBoxSource] in the beginning of the code how do I set it up?

??? Dim strLstBoxSource (1 to ?, 1 to 5) as String ???

The source is empty to begin with but as the user adds rows via the form,
the Array grows.

1) How to set the original declaration?

2) When Where and How to ReDim and Preserve the new List for the listbox?
 
C

CS Hayes

Boy, I am making progress but not there yet.

here's what I ended up with.

'I learned that I have to define a "dynamic" array

Dim varListBoxSource () as Variant

'Once I get to the place to define the boundaries and fill the variable
' I get this error <subscript out of range>

If IsNull(varListBoxSource) = True Then

'varListBoxSource will be Null if this is the first time

ReDim varListBoxSource(1 To 1, 1 To 5) As Variant
Else: ReDim Preserve varListBoxSource(UBound(varListBoxSource) + 1)

any suggestions?

--
Chris Hayes
Still a beginner (only 12 years)


CS Hayes said:
Ok, how about a growing array
I'm keeping the code to the form right now

I have a list box with 5 columns. 1) start (string), 2) finish (string), 3)
difference
(integer), 4) multiplier (double), 5) total (double)

These are all calculated in a Private sub on the on click event from the form.

If I declare a variable for the listbox row source [.List () =
strLstBoxSource] in the beginning of the code how do I set it up?

??? Dim strLstBoxSource (1 to ?, 1 to 5) as String ???

The source is empty to begin with but as the user adds rows via the form,
the Array grows.

1) How to set the original declaration?

2) When Where and How to ReDim and Preserve the new List for the listbox?

--
Chris Hayes
Still a beginner (only 12 years)


CS Hayes said:
I need to store a list box's source in code (or a hidden table? I don't
know.) The list grows. The user can add an item via form to the list.

I have a form and on the add click I want to add a 5 column entry to the
listbox

I know of the "additem" key word but how do I add the 5 items to one row

My items are like this: start (string), finish (string), difference
(integer), multiplier (double), total (double)
 
P

Perry

Here are two examples filling a listbox
1) using the additem() method without arrays
2) using an array to pass to the Column property of a listbox
(both same result)

example 1
With Me.ListBox1
.AddItem "apples1"
.List(.ListCount - 1, 1) = "pears1"
.List(.ListCount - 1, 2) = "bananas1"

.AddItem "apples2"
.List(.ListCount - 1, 1) = "pears2"
.List(.ListCount - 1, 2) = "bananas2"

.AddItem "apples3"
.List(.ListCount - 1, 1) = "pears3"
.List(.ListCount - 1, 2) = "bananas3"
End With

example 2
Dim icnt As Integer
Dim arr()
For x = 1 To 3
ReDim Preserve arr(2, icnt)
arr(0, icnt) = "apples" & (icnt + 1)
arr(1, icnt) = "pears" & (icnt + 1)
arr(2, icnt) = "bananas" & (icnt + 1)
icnt = icnt + 1
Next
Me.ListBox1.Column = arr

--
Krgrds,
Perry

System:
Vista/Office Ultimate
VS2005/VSTO2005 SE
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top