Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 570
  • Last Modified:

Array of TextBoxes VB.NET

In Visual Basic .NET:

I'm trying to initialize an array of TextBoxes...
    Dim txtBoxes as TextBox() = {txtBox1, txtBox2, txtBox3, txtBox4}
But when I try to use one of the elements like this...
    Dim str as String = txtBoxes(0).Text
...it doesn't work.  But when I initialize the array this way...
    Dim txtBoxes(2) as TextBox
    txtBoxes(0) = txtBox1
    txtBoxes(1) = txtBox2
    txtBoxes(2) = txtBox3
...the statement above does work.  Is there any way I can use the first initialization with an array of TextBoxes?
0
walang
Asked:
walang
  • 4
  • 4
  • 2
  • +1
3 Solutions
 
Bob LearnedCommented:
Why didn't the first form work?  I tried it, and it worked just fine for me.

Bob
0
 
jpaulinoCommented:
Dim txtBoxes() as TextBox = {txtBox1, txtBox2, txtBox3, txtBox4}
0
 
jpaulinoCommented:
txtBoxes is your array
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
walangAuthor Commented:
You're right...I just figured out what the real problem is now...I was initializing the array before the loading of the form, in a global variable.  I'm guessing this doesn't work, only locally within the subroutines?
0
 
jpaulinoCommented:
yes, that's the problem. I've tried and I had the error to
0
 
walangAuthor Commented:
So it can't be done globally...ahh man :(
0
 
jpaulinoCommented:
But if you want you can add the array like string and then get using System.Reflection

Dim txtBoxes() as String = {"txtBox1","txtBox2", "txtBox3","txtBox4"}
Public Function GetControlByName(ByVal Name As String) As Control
        Dim info As System.Reflection.FieldInfo = Me.GetType().GetField("_" & Name, System.Reflection.BindingFlags.NonPublic Or System.Reflection.BindingFlags.Instance Or System.Reflection.BindingFlags.Public Or System.Reflection.BindingFlags.IgnoreCase)
        If info Is Nothing Then
            Return Nothing
        Else
            Dim obj As Object = info.GetValue(Me)
            Return obj
        End If
    End Function

and then in your Sub


MsgBox(CType(GetControlByName(txtBoxes(1)), TextBox).Text)
0
 
PaulHewsCommented:
If you declare your array as global it can be done, but not before the textboxes have been instantiated by loading the form...
0
 
walangAuthor Commented:
Yeah I think I'm going to have to declare the array globally, then instantiate them within a subroutine.  I don't want to do anything too complex.
0
 
PaulHewsCommented:
If you do that, then unless the form they reside on is the main form, you would have to consider what happens when the form is unloaded.  It looks like the reference still has a valid textbox object, even when the form the textbox is physically created on has been unloaded.  So you might want to clear your array once you close your form to prevent weirdness.
0
 
walangAuthor Commented:
It will be in the same form, it's only going to be used for 1 of my forms, so I think it should be ok.
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 4
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now