Appending text to an array of textboxes

J

Jacob.Bruxer

Hi,
I want to be able to append text using a For loop to each textbox in an
array of textboxes that I've created, called tBoxes(). Basically I
want to add a number of spaces to each textbox in the array. ie. If
spaces(0) = 3, I'd want to add 3 spaces (" ") to tBoxes(0), if
spaces(1) = 4, I'd want to add 4 spaces (" ") to tBoxes(1), and so
on. My code looks something like this...

Dim i as integer
Dim j as integer
Dim m as integer

' I determine value j, which is the length of each of my arrays
j = some integer

Dim tBoxes(j-1) as textbox
Dim spaces(j-1) as integer

' I determine the number of spaces and assign integer values to the
array spaces(i)

' For each textbox, I try to append a space for each integer in
spaces(i), but it doesn't work.
For i = 0 To j - 1
For m = 1 To spaces(i)
tBoxes(i).AppendText(" ")
Next
Next

When I run the code I get an error: "NullReferenceException was
unhandled" and "Object reference not set to an instance of an object."

I hope this is all clear. I'm new to programming so any suggestions or
ideas on what I'm doing wrong would be most appreciated. Thanks.
 
A

Andrew Backer

Are you actually creating the textboxes anywhere? It just look like
you are initializing an array that is of type textbox. Try something
like this

Dim x(12) As TextBox
For i As Integer = 1 To 12
x(i) = New TextBox()
Next

HTH,
Andrew
 
G

guigui

When I run the code I get an error: "NullReferenceException was
unhandled" and "Object reference not set to an instance of an object."
i'm not sure using array is a good way prefer collections

try

Sub ess()

Dim j As Integer

Dim i As Integer

Dim tbx As TextBox

Dim tbcols As New Collection

'filling the collection

For i = 1 To j

tbx = New TextBox

tbcols.Add(tbx, i.ToString)

Next

'look into the collection

For Each tbx In tbcols

Next



End Sub
 

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