Monday, March 26, 2012

Working with controls in an array?

I am trying to make the transition from VB6/VBA to VB.NET in an ASP.NET web application. One of the things that I just cannot figure out how to do is work with controls that are called out in an array.

For example, this should work in VB6/VBA (from memory, may contain errors):

Public Sub CheckTextboxes()
Dim MyTextboxes as Variant, RequiredTextboxes as Variant, BadTextboxes as Variant
Dim l as Long

'MyTextboxes is an array of all textboxes on the form:
MyTextboxes = Array(Textbox1, Textbox2, Textbox3, Textbox4)

'RequiredTextboxes is an array of only the textboxes that I
'want to check to see if they contain text:
'In this case, I only want to see if Textbox2, and Textbox3 contain text:
RequiredTextboxes = Array(1, 2)

'BadTextboxes is an empty array that I will populate with all textbox.name(s)
'that I find to be empty:
BadTextboxes = Array()

'Loop through only the required textboxes, in this case 1 and 2, or Textbox2 and
'Textbox3 (because these arrays start out at element 0). *** This is where I have
'problems in VB.NET in an ASP.NET web application. ***
For l = lbound(RequiredTextboxes) to ubound(RequiredTextboxes)

'Is the textbox.text blank?
If Trim(MyTextboxes(RequiredTextboxes(l)).text) = "" then

'If so, add an element to BadTextboxes:
Redim Preserve BadTextboxes(lbound(BadTextboxes) to ubound(BadTextboxes) + 1)

'Assign the newly created element (at the end, or upper bound) to be the name
'of the textbox that was empty:
BadTextboxes(ubound(BadTextboxes)) = MyTextboxes(RequiredTextboxes(l)).name
End If
Next l

'BadTextboxes is now an array that is populated with all the names of every textbox
'that was found to be empty for me to do something with later...
End Sub

Now let's break this down into a more simple problem (in VB.NET):

Public MyTextboxes() as Object = {Textbox1, Textbox2, Textbox3}

Sub CheckTextBoxes
Dim l as Long

'All I want to do in this simple example is echo/response.write the TEXT
'of every textbox in my array:
For l = lbound(MyTextboxes) to ubound(MyTextboxes)

'*** THIS NEVER WORKS, NO MATTER WHAT I TRY FOR SYNTAX. ***
'I have tried controls, control.controls(), Ctype, etc. I cannot get this to
'work without an error:
response.write(MyTextboxes(l).text)
Next l
End Sub

I don't want to dynamically add textboxes, I just need to be able to control various controls when they are collected in an array and I just cannot get the syntax to work.

Many thanks in advance!

- Arokh

PS - Sorry if this is hard to read, I don't think my code spacing will come through correctly.I think I can explain this better. I want to check all my fields to see if they are blank and if so, change the forecolor of the corresponding label to red. I know I should just use validator controls, but if I can accomplish this, it will make the code *much* smaller. Please help if you know what I'm doing wrong, thanks!


Sub FormSubmit()
Dim MyFields() = {Field1, Field2} 'Two textboxes on the form.
Dim MyLabels() = {Label1, Label2} 'Two labels on the form.
Dim l as Long

For l = lbound(MyFields) to ubound(MyFields)
if Trim(MyFields(l).text) = "" Then '*** Doesn't work, can't get syntax right. ***
MyLabels(l).forecolor = system.drawing.color.fromname("Red")
Else
MyLabels(l).forecolor = system.drawing.color.fromname("Black")
End If
Next l
End Sub

0 comments:

Post a Comment